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