1 //===- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils -----*- C++ -*-===//
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 
14 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
15 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
16 
17 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/Dominators.h"
23 #include <cassert>
24 
25 namespace llvm {
26 class BranchInst;
27 class LandingPadInst;
28 class Loop;
29 class PHINode;
30 template <typename PtrType> class SmallPtrSetImpl;
31 class BlockFrequencyInfo;
32 class BranchProbabilityInfo;
33 class DomTreeUpdater;
34 class Function;
35 class IRBuilderBase;
36 class LoopInfo;
37 class MDNode;
38 class MemoryDependenceResults;
39 class MemorySSAUpdater;
40 class PostDominatorTree;
41 class ReturnInst;
42 class TargetLibraryInfo;
43 class Value;
44 
45 /// Replace contents of every block in \p BBs with single unreachable
46 /// instruction. If \p Updates is specified, collect all necessary DT updates
47 /// into this vector. If \p KeepOneInputPHIs is true, one-input Phis in
48 /// successors of blocks being deleted will be preserved.
49 void detachDeadBlocks(ArrayRef <BasicBlock *> BBs,
50                       SmallVectorImpl<DominatorTree::UpdateType> *Updates,
51                       bool KeepOneInputPHIs = false);
52 
53 /// Delete the specified block, which must have no predecessors.
54 void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU = nullptr,
55                      bool KeepOneInputPHIs = false);
56 
57 /// Delete the specified blocks from \p BB. The set of deleted blocks must have
58 /// no predecessors that are not being deleted themselves. \p BBs must have no
59 /// duplicating blocks. If there are loops among this set of blocks, all
60 /// relevant loop info updates should be done before this function is called.
61 /// If \p KeepOneInputPHIs is true, one-input Phis in successors of blocks
62 /// being deleted will be preserved.
63 void DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs,
64                       DomTreeUpdater *DTU = nullptr,
65                       bool KeepOneInputPHIs = false);
66 
67 /// Delete all basic blocks from \p F that are not reachable from its entry
68 /// node. If \p KeepOneInputPHIs is true, one-input Phis in successors of
69 /// blocks being deleted will be preserved.
70 bool EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU = nullptr,
71                                 bool KeepOneInputPHIs = false);
72 
73 /// We know that BB has one predecessor. If there are any single-entry PHI nodes
74 /// in it, fold them away. This handles the case when all entries to the PHI
75 /// nodes in a block are guaranteed equal, such as when the block has exactly
76 /// one predecessor.
77 bool FoldSingleEntryPHINodes(BasicBlock *BB,
78                              MemoryDependenceResults *MemDep = nullptr);
79 
80 /// Examine each PHI in the given block and delete it if it is dead. Also
81 /// recursively delete any operands that become dead as a result. This includes
82 /// tracing the def-use list from the PHI to see if it is ultimately unused or
83 /// if it reaches an unused cycle. Return true if any PHIs were deleted.
84 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr,
85                     MemorySSAUpdater *MSSAU = nullptr);
86 
87 /// Attempts to merge a block into its predecessor, if possible. The return
88 /// value indicates success or failure.
89 /// By default do not merge blocks if BB's predecessor has multiple successors.
90 /// If PredecessorWithTwoSuccessors = true, the blocks can only be merged
91 /// if BB's Pred has a branch to BB and to AnotherBB, and BB has a single
92 /// successor Sing. In this case the branch will be updated with Sing instead of
93 /// BB, and BB will still be merged into its predecessor and removed.
94 /// If \p DT is not nullptr, update it directly; in that case, DTU must be
95 /// nullptr.
96 bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU = nullptr,
97                                LoopInfo *LI = nullptr,
98                                MemorySSAUpdater *MSSAU = nullptr,
99                                MemoryDependenceResults *MemDep = nullptr,
100                                bool PredecessorWithTwoSuccessors = false,
101                                DominatorTree *DT = nullptr);
102 
103 /// Merge block(s) sucessors, if possible. Return true if at least two
104 /// of the blocks were merged together.
105 /// In order to merge, each block must be terminated by an unconditional
106 /// branch. If L is provided, then the blocks merged into their predecessors
107 /// must be in L. In addition, This utility calls on another utility:
108 /// MergeBlockIntoPredecessor. Blocks are successfully merged when the call to
109 /// MergeBlockIntoPredecessor returns true.
110 bool MergeBlockSuccessorsIntoGivenBlocks(
111     SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L = nullptr,
112     DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr);
113 
114 /// Try to remove redundant dbg.value instructions from given basic block.
115 /// Returns true if at least one instruction was removed. Remove redundant
116 /// pseudo ops when RemovePseudoOp is true.
117 bool RemoveRedundantDbgInstrs(BasicBlock *BB);
118 
119 /// Replace all uses of an instruction (specified by BI) with a value, then
120 /// remove and delete the original instruction.
121 void ReplaceInstWithValue(BasicBlock::iterator &BI, Value *V);
122 
123 /// Replace the instruction specified by BI with the instruction specified by I.
124 /// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The
125 /// original instruction is deleted and BI is updated to point to the new
126 /// instruction.
127 void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI,
128                          Instruction *I);
129 
130 /// Replace the instruction specified by From with the instruction specified by
131 /// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc.
132 void ReplaceInstWithInst(Instruction *From, Instruction *To);
133 
134 /// Check if we can prove that all paths starting from this block converge
135 /// to a block that either has a @llvm.experimental.deoptimize call
136 /// prior to its terminating return instruction or is terminated by unreachable.
137 /// All blocks in the traversed sequence must have an unique successor, maybe
138 /// except for the last one.
139 bool IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB);
140 
141 /// Option class for critical edge splitting.
142 ///
143 /// This provides a builder interface for overriding the default options used
144 /// during critical edge splitting.
145 struct CriticalEdgeSplittingOptions {
146   DominatorTree *DT;
147   PostDominatorTree *PDT;
148   LoopInfo *LI;
149   MemorySSAUpdater *MSSAU;
150   bool MergeIdenticalEdges = false;
151   bool KeepOneInputPHIs = false;
152   bool PreserveLCSSA = false;
153   bool IgnoreUnreachableDests = false;
154   /// SplitCriticalEdge is guaranteed to preserve loop-simplify form if LI is
155   /// provided. If it cannot be preserved, no splitting will take place. If it
156   /// is not set, preserve loop-simplify form if possible.
157   bool PreserveLoopSimplify = true;
158 
159   CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr,
160                                LoopInfo *LI = nullptr,
161                                MemorySSAUpdater *MSSAU = nullptr,
162                                PostDominatorTree *PDT = nullptr)
163       : DT(DT), PDT(PDT), LI(LI), MSSAU(MSSAU) {}
164 
165   CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
166     MergeIdenticalEdges = true;
167     return *this;
168   }
169 
170   CriticalEdgeSplittingOptions &setKeepOneInputPHIs() {
171     KeepOneInputPHIs = true;
172     return *this;
173   }
174 
175   CriticalEdgeSplittingOptions &setPreserveLCSSA() {
176     PreserveLCSSA = true;
177     return *this;
178   }
179 
180   CriticalEdgeSplittingOptions &setIgnoreUnreachableDests() {
181     IgnoreUnreachableDests = true;
182     return *this;
183   }
184 
185   CriticalEdgeSplittingOptions &unsetPreserveLoopSimplify() {
186     PreserveLoopSimplify = false;
187     return *this;
188   }
189 };
190 
191 /// When a loop exit edge is split, LCSSA form may require new PHIs in the new
192 /// exit block. This function inserts the new PHIs, as needed. Preds is a list
193 /// of preds inside the loop, SplitBB is the new loop exit block, and DestBB is
194 /// the old loop exit, now the successor of SplitBB.
195 void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
196                                 BasicBlock *SplitBB, BasicBlock *DestBB);
197 
198 /// If this edge is a critical edge, insert a new node to split the critical
199 /// edge. This will update the analyses passed in through the option struct.
200 /// This returns the new block if the edge was split, null otherwise.
201 ///
202 /// If MergeIdenticalEdges in the options struct is true (not the default),
203 /// *all* edges from TI to the specified successor will be merged into the same
204 /// critical edge block. This is most commonly interesting with switch
205 /// instructions, which may have many edges to any one destination.  This
206 /// ensures that all edges to that dest go to one block instead of each going
207 /// to a different block, but isn't the standard definition of a "critical
208 /// edge".
209 ///
210 /// It is invalid to call this function on a critical edge that starts at an
211 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
212 /// program because the address of the new block won't be the one that is jumped
213 /// to.
214 BasicBlock *SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
215                               const CriticalEdgeSplittingOptions &Options =
216                                   CriticalEdgeSplittingOptions(),
217                               const Twine &BBName = "");
218 
219 /// If it is known that an edge is critical, SplitKnownCriticalEdge can be
220 /// called directly, rather than calling SplitCriticalEdge first.
221 BasicBlock *SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum,
222                                    const CriticalEdgeSplittingOptions &Options =
223                                        CriticalEdgeSplittingOptions(),
224                                    const Twine &BBName = "");
225 
226 /// If an edge from Src to Dst is critical, split the edge and return true,
227 /// otherwise return false. This method requires that there be an edge between
228 /// the two blocks. It updates the analyses passed in the options struct
229 inline BasicBlock *
230 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
231                   const CriticalEdgeSplittingOptions &Options =
232                       CriticalEdgeSplittingOptions()) {
233   Instruction *TI = Src->getTerminator();
234   unsigned i = 0;
235   while (true) {
236     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
237     if (TI->getSuccessor(i) == Dst)
238       return SplitCriticalEdge(TI, i, Options);
239     ++i;
240   }
241 }
242 
243 /// Loop over all of the edges in the CFG, breaking critical edges as they are
244 /// found. Returns the number of broken edges.
245 unsigned SplitAllCriticalEdges(Function &F,
246                                const CriticalEdgeSplittingOptions &Options =
247                                    CriticalEdgeSplittingOptions());
248 
249 /// Split the edge connecting the specified blocks, and return the newly created
250 /// basic block between \p From and \p To.
251 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
252                       DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
253                       MemorySSAUpdater *MSSAU = nullptr,
254                       const Twine &BBName = "");
255 
256 /// Sets the unwind edge of an instruction to a particular successor.
257 void setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ);
258 
259 /// Replaces all uses of OldPred with the NewPred block in all PHINodes in a
260 /// block.
261 void updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,
262                     BasicBlock *NewPred, PHINode *Until = nullptr);
263 
264 /// Split the edge connect the specficed blocks in the case that \p Succ is an
265 /// Exception Handling Block
266 BasicBlock *ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,
267                              LandingPadInst *OriginalPad = nullptr,
268                              PHINode *LandingPadReplacement = nullptr,
269                              const CriticalEdgeSplittingOptions &Options =
270                                  CriticalEdgeSplittingOptions(),
271                              const Twine &BBName = "");
272 
273 /// Split the specified block at the specified instruction.
274 ///
275 /// If \p Before is true, splitBlockBefore handles the block
276 /// splitting. Otherwise, execution proceeds as described below.
277 ///
278 /// Everything before \p SplitPt stays in \p Old and everything starting with \p
279 /// SplitPt moves to a new block. The two blocks are joined by an unconditional
280 /// branch. The new block with name \p BBName is returned.
281 ///
282 /// FIXME: deprecated, switch to the DomTreeUpdater-based one.
283 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT,
284                        LoopInfo *LI = nullptr,
285                        MemorySSAUpdater *MSSAU = nullptr,
286                        const Twine &BBName = "", bool Before = false);
287 
288 /// Split the specified block at the specified instruction.
289 ///
290 /// If \p Before is true, splitBlockBefore handles the block
291 /// splitting. Otherwise, execution proceeds as described below.
292 ///
293 /// Everything before \p SplitPt stays in \p Old and everything starting with \p
294 /// SplitPt moves to a new block. The two blocks are joined by an unconditional
295 /// branch. The new block with name \p BBName is returned.
296 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
297                        DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
298                        MemorySSAUpdater *MSSAU = nullptr,
299                        const Twine &BBName = "", bool Before = false);
300 
301 /// Split the specified block at the specified instruction \p SplitPt.
302 /// All instructions before \p SplitPt are moved to a new block and all
303 /// instructions after \p SplitPt stay in the old block. The new block and the
304 /// old block are joined by inserting an unconditional branch to the end of the
305 /// new block. The new block with name \p BBName is returned.
306 BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
307                              DomTreeUpdater *DTU, LoopInfo *LI,
308                              MemorySSAUpdater *MSSAU, const Twine &BBName = "");
309 
310 /// This method introduces at least one new basic block into the function and
311 /// moves some of the predecessors of BB to be predecessors of the new block.
312 /// The new predecessors are indicated by the Preds array. The new block is
313 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors
314 /// from Preds are now pointing.
315 ///
316 /// If BB is a landingpad block then additional basicblock might be introduced.
317 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
318 /// details on this case.
319 ///
320 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
321 /// no other analyses. In particular, it does not preserve LoopSimplify
322 /// (because it's complicated to handle the case where one of the edges being
323 /// split is an exit of a loop with other exits).
324 ///
325 /// FIXME: deprecated, switch to the DomTreeUpdater-based one.
326 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
327                                    const char *Suffix, DominatorTree *DT,
328                                    LoopInfo *LI = nullptr,
329                                    MemorySSAUpdater *MSSAU = nullptr,
330                                    bool PreserveLCSSA = false);
331 
332 /// This method introduces at least one new basic block into the function and
333 /// moves some of the predecessors of BB to be predecessors of the new block.
334 /// The new predecessors are indicated by the Preds array. The new block is
335 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors
336 /// from Preds are now pointing.
337 ///
338 /// If BB is a landingpad block then additional basicblock might be introduced.
339 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
340 /// details on this case.
341 ///
342 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
343 /// no other analyses. In particular, it does not preserve LoopSimplify
344 /// (because it's complicated to handle the case where one of the edges being
345 /// split is an exit of a loop with other exits).
346 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
347                                    const char *Suffix,
348                                    DomTreeUpdater *DTU = nullptr,
349                                    LoopInfo *LI = nullptr,
350                                    MemorySSAUpdater *MSSAU = nullptr,
351                                    bool PreserveLCSSA = false);
352 
353 /// This method transforms the landing pad, OrigBB, by introducing two new basic
354 /// blocks into the function. One of those new basic blocks gets the
355 /// predecessors listed in Preds. The other basic block gets the remaining
356 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both
357 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and
358 /// 'Suffix2', and are returned in the NewBBs vector.
359 ///
360 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
361 /// no other analyses. In particular, it does not preserve LoopSimplify
362 /// (because it's complicated to handle the case where one of the edges being
363 /// split is an exit of a loop with other exits).
364 ///
365 /// FIXME: deprecated, switch to the DomTreeUpdater-based one.
366 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
367                                  ArrayRef<BasicBlock *> Preds,
368                                  const char *Suffix, const char *Suffix2,
369                                  SmallVectorImpl<BasicBlock *> &NewBBs,
370                                  DominatorTree *DT, LoopInfo *LI = nullptr,
371                                  MemorySSAUpdater *MSSAU = nullptr,
372                                  bool PreserveLCSSA = false);
373 
374 /// This method transforms the landing pad, OrigBB, by introducing two new basic
375 /// blocks into the function. One of those new basic blocks gets the
376 /// predecessors listed in Preds. The other basic block gets the remaining
377 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both
378 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and
379 /// 'Suffix2', and are returned in the NewBBs vector.
380 ///
381 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
382 /// no other analyses. In particular, it does not preserve LoopSimplify
383 /// (because it's complicated to handle the case where one of the edges being
384 /// split is an exit of a loop with other exits).
385 void SplitLandingPadPredecessors(
386     BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix,
387     const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
388     DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
389     MemorySSAUpdater *MSSAU = nullptr, bool PreserveLCSSA = false);
390 
391 /// This method duplicates the specified return instruction into a predecessor
392 /// which ends in an unconditional branch. If the return instruction returns a
393 /// value defined by a PHI, propagate the right value into the return. It
394 /// returns the new return instruction in the predecessor.
395 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
396                                        BasicBlock *Pred,
397                                        DomTreeUpdater *DTU = nullptr);
398 
399 /// Split the containing block at the specified instruction - everything before
400 /// SplitBefore stays in the old basic block, and the rest of the instructions
401 /// in the BB are moved to a new block. The two blocks are connected by a
402 /// conditional branch (with value of Cmp being the condition).
403 /// Before:
404 ///   Head
405 ///   SplitBefore
406 ///   Tail
407 /// After:
408 ///   Head
409 ///   if (Cond)
410 ///     ThenBlock
411 ///   SplitBefore
412 ///   Tail
413 ///
414 /// If \p ThenBlock is not specified, a new block will be created for it.
415 /// If \p Unreachable is true, the newly created block will end with
416 /// UnreachableInst, otherwise it branches to Tail.
417 /// Returns the NewBasicBlock's terminator.
418 ///
419 /// Updates DTU and LI if given.
420 Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
421                                        bool Unreachable,
422                                        MDNode *BranchWeights = nullptr,
423                                        DomTreeUpdater *DTU = nullptr,
424                                        LoopInfo *LI = nullptr,
425                                        BasicBlock *ThenBlock = nullptr);
426 
427 /// Similar to SplitBlockAndInsertIfThen, but the inserted block is on the false
428 /// path of the branch.
429 Instruction *SplitBlockAndInsertIfElse(Value *Cond, Instruction *SplitBefore,
430                                        bool Unreachable,
431                                        MDNode *BranchWeights = nullptr,
432                                        DomTreeUpdater *DTU = nullptr,
433                                        LoopInfo *LI = nullptr,
434                                        BasicBlock *ElseBlock = nullptr);
435 
436 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
437 /// but also creates the ElseBlock.
438 /// Before:
439 ///   Head
440 ///   SplitBefore
441 ///   Tail
442 /// After:
443 ///   Head
444 ///   if (Cond)
445 ///     ThenBlock
446 ///   else
447 ///     ElseBlock
448 ///   SplitBefore
449 ///   Tail
450 ///
451 /// Updates DT if given.
452 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
453                                    Instruction **ThenTerm,
454                                    Instruction **ElseTerm,
455                                    MDNode *BranchWeights = nullptr,
456                                    DomTreeUpdater *DTU = nullptr,
457                                    LoopInfo *LI = nullptr);
458 
459 /// Split the containing block at the specified instruction - everything before
460 /// SplitBefore stays in the old basic block, and the rest of the instructions
461 /// in the BB are moved to a new block. The two blocks are connected by a
462 /// conditional branch (with value of Cmp being the condition).
463 /// Before:
464 ///   Head
465 ///   SplitBefore
466 ///   Tail
467 /// After:
468 ///   Head
469 ///   if (Cond)
470 ///     TrueBlock
471 ///   else
472 ////    FalseBlock
473 ///   SplitBefore
474 ///   Tail
475 ///
476 /// If \p ThenBlock is null, the resulting CFG won't contain the TrueBlock. If
477 /// \p ThenBlock is non-null and points to non-null BasicBlock pointer, that
478 /// block will be inserted as the TrueBlock. Otherwise a new block will be
479 /// created. Likewise for the \p ElseBlock parameter.
480 /// If \p UnreachableThen or \p UnreachableElse is true, the corresponding newly
481 /// created blocks will end with UnreachableInst, otherwise with branches to
482 /// Tail. The function will not modify existing basic blocks passed to it. The
483 /// caller must ensure that Tail is reachable from Head.
484 /// Returns the newly created blocks in \p ThenBlock and \p ElseBlock.
485 /// Updates DTU and LI if given.
486 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
487                                    BasicBlock **ThenBlock,
488                                    BasicBlock **ElseBlock,
489                                    bool UnreachableThen = false,
490                                    bool UnreachableElse = false,
491                                    MDNode *BranchWeights = nullptr,
492                                    DomTreeUpdater *DTU = nullptr,
493                                    LoopInfo *LI = nullptr);
494 
495 /// Insert a for (int i = 0; i < End; i++) loop structure (with the exception
496 /// that \p End is assumed > 0, and thus not checked on entry) at \p
497 /// SplitBefore.  Returns the first insert point in the loop body, and the
498 /// PHINode for the induction variable (i.e. "i" above).
499 std::pair<Instruction*, Value*>
500 SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore);
501 
502 /// Utility function for performing a given action on each lane of a vector
503 /// with \p EC elements.  To simplify porting legacy code, this defaults to
504 /// unrolling the implied loop for non-scalable element counts, but this is
505 /// not considered to be part of the contract of this routine, and is
506 /// expected to change in the future. The callback takes as arguments an
507 /// IRBuilder whose insert point is correctly set for instantiating the
508 /// given index, and a value which is (at runtime) the index to access.
509 /// This index *may* be a constant.
510 void SplitBlockAndInsertForEachLane(ElementCount EC, Type *IndexTy,
511     Instruction *InsertBefore,
512     std::function<void(IRBuilderBase&, Value*)> Func);
513 
514 /// Utility function for performing a given action on each lane of a vector
515 /// with \p EVL effective length. EVL is assumed > 0. To simplify porting legacy
516 /// code, this defaults to unrolling the implied loop for non-scalable element
517 /// counts, but this is not considered to be part of the contract of this
518 /// routine, and is expected to change in the future. The callback takes as
519 /// arguments an IRBuilder whose insert point is correctly set for instantiating
520 /// the given index, and a value which is (at runtime) the index to access. This
521 /// index *may* be a constant.
522 void SplitBlockAndInsertForEachLane(
523     Value *End, Instruction *InsertBefore,
524     std::function<void(IRBuilderBase &, Value *)> Func);
525 
526 /// Check whether BB is the merge point of a if-region.
527 /// If so, return the branch instruction that determines which entry into
528 /// BB will be taken.  Also, return by references the block that will be
529 /// entered from if the condition is true, and the block that will be
530 /// entered if the condition is false.
531 ///
532 /// This does no checking to see if the true/false blocks have large or unsavory
533 /// instructions in them.
534 BranchInst *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
535                            BasicBlock *&IfFalse);
536 
537 // Split critical edges where the source of the edge is an indirectbr
538 // instruction. This isn't always possible, but we can handle some easy cases.
539 // This is useful because MI is unable to split such critical edges,
540 // which means it will not be able to sink instructions along those edges.
541 // This is especially painful for indirect branches with many successors, where
542 // we end up having to prepare all outgoing values in the origin block.
543 //
544 // Our normal algorithm for splitting critical edges requires us to update
545 // the outgoing edges of the edge origin block, but for an indirectbr this
546 // is hard, since it would require finding and updating the block addresses
547 // the indirect branch uses. But if a block only has a single indirectbr
548 // predecessor, with the others being regular branches, we can do it in a
549 // different way.
550 // Say we have A -> D, B -> D, I -> D where only I -> D is an indirectbr.
551 // We can split D into D0 and D1, where D0 contains only the PHIs from D,
552 // and D1 is the D block body. We can then duplicate D0 as D0A and D0B, and
553 // create the following structure:
554 // A -> D0A, B -> D0A, I -> D0B, D0A -> D1, D0B -> D1
555 // If BPI and BFI aren't non-null, BPI/BFI will be updated accordingly.
556 // When `IgnoreBlocksWithoutPHI` is set to `true` critical edges leading to a
557 // block without phi-instructions will not be split.
558 bool SplitIndirectBrCriticalEdges(Function &F, bool IgnoreBlocksWithoutPHI,
559                                   BranchProbabilityInfo *BPI = nullptr,
560                                   BlockFrequencyInfo *BFI = nullptr);
561 
562 /// Given a set of incoming and outgoing blocks, create a "hub" such that every
563 /// edge from an incoming block InBB to an outgoing block OutBB is now split
564 /// into two edges, one from InBB to the hub and another from the hub to
565 /// OutBB. The hub consists of a series of guard blocks, one for each outgoing
566 /// block. Each guard block conditionally branches to the corresponding outgoing
567 /// block, or the next guard block in the chain. These guard blocks are returned
568 /// in the argument vector.
569 ///
570 /// Since the control flow edges from InBB to OutBB have now been replaced, the
571 /// function also updates any PHINodes in OutBB. For each such PHINode, the
572 /// operands corresponding to incoming blocks are moved to a new PHINode in the
573 /// hub, and the hub is made an operand of the original PHINode.
574 ///
575 /// Input CFG:
576 /// ----------
577 ///
578 ///                    Def
579 ///                     |
580 ///                     v
581 ///           In1      In2
582 ///            |        |
583 ///            |        |
584 ///            v        v
585 ///  Foo ---> Out1     Out2
586 ///                     |
587 ///                     v
588 ///                    Use
589 ///
590 ///
591 /// Create hub: Incoming = {In1, In2}, Outgoing = {Out1, Out2}
592 /// ----------------------------------------------------------
593 ///
594 ///             Def
595 ///              |
596 ///              v
597 ///  In1        In2          Foo
598 ///   |    Hub   |            |
599 ///   |    + - - | - - +      |
600 ///   |    '     v     '      V
601 ///   +------> Guard1 -----> Out1
602 ///        '     |     '
603 ///        '     v     '
604 ///        '   Guard2 -----> Out2
605 ///        '           '      |
606 ///        + - - - - - +      |
607 ///                           v
608 ///                          Use
609 ///
610 /// Limitations:
611 /// -----------
612 /// 1. This assumes that all terminators in the CFG are direct branches (the
613 ///    "br" instruction). The presence of any other control flow such as
614 ///    indirectbr, switch or callbr will cause an assert.
615 ///
616 /// 2. The updates to the PHINodes are not sufficient to restore SSA
617 ///    form. Consider a definition Def, its use Use, incoming block In2 and
618 ///    outgoing block Out2, such that:
619 ///    a. In2 is reachable from D or contains D.
620 ///    b. U is reachable from Out2 or is contained in Out2.
621 ///    c. U is not a PHINode if U is contained in Out2.
622 ///
623 ///    Clearly, Def dominates Out2 since the program is valid SSA. But when the
624 ///    hub is introduced, there is a new path through the hub along which Use is
625 ///    reachable from entry without passing through Def, and SSA is no longer
626 ///    valid. To fix this, we need to look at all the blocks post-dominated by
627 ///    the hub on the one hand, and dominated by Out2 on the other. This is left
628 ///    for the caller to accomplish, since each specific use of this function
629 ///    may have additional information which simplifies this fixup. For example,
630 ///    see restoreSSA() in the UnifyLoopExits pass.
631 BasicBlock *CreateControlFlowHub(
632     DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
633     const SetVector<BasicBlock *> &Predecessors,
634     const SetVector<BasicBlock *> &Successors, const StringRef Prefix,
635     std::optional<unsigned> MaxControlFlowBooleans = std::nullopt);
636 
637 // Utility function for inverting branch condition and for swapping its
638 // successors
639 void InvertBranch(BranchInst *PBI, IRBuilderBase &Builder);
640 
641 } // end namespace llvm
642 
643 #endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
644