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