LLVM  14.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"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/CFG.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include <cassert>
27 
28 namespace llvm {
29 
30 class BlockFrequencyInfo;
31 class BranchProbabilityInfo;
32 class DominatorTree;
33 class DomTreeUpdater;
34 class Function;
35 class Instruction;
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 DetatchDeadBlocks(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.
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 bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU = nullptr,
95  LoopInfo *LI = nullptr,
96  MemorySSAUpdater *MSSAU = nullptr,
97  MemoryDependenceResults *MemDep = nullptr,
98  bool PredecessorWithTwoSuccessors = false);
99 
100 /// Merge block(s) sucessors, if possible. Return true if at least two
101 /// of the blocks were merged together.
102 /// In order to merge, each block must be terminated by an unconditional
103 /// branch. If L is provided, then the blocks merged into their predecessors
104 /// must be in L. In addition, This utility calls on another utility:
105 /// MergeBlockIntoPredecessor. Blocks are successfully merged when the call to
106 /// MergeBlockIntoPredecessor returns true.
108  SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L = nullptr,
109  DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr);
110 
111 /// Try to remove redundant dbg.value instructions from given basic block.
112 /// Returns true if at least one instruction was removed. Remove redundant
113 /// pseudo ops when RemovePseudoOp is true.
115 
116 /// Replace all uses of an instruction (specified by BI) with a value, then
117 /// remove and delete the original instruction.
119  BasicBlock::iterator &BI, Value *V);
120 
121 /// Replace the instruction specified by BI with the instruction specified by I.
122 /// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The
123 /// original instruction is deleted and BI is updated to point to the new
124 /// instruction.
126  BasicBlock::iterator &BI, Instruction *I);
127 
128 /// Replace the instruction specified by From with the instruction specified by
129 /// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc.
130 void ReplaceInstWithInst(Instruction *From, Instruction *To);
131 
132 /// Option class for critical edge splitting.
133 ///
134 /// This provides a builder interface for overriding the default options used
135 /// during critical edge splitting.
141  bool MergeIdenticalEdges = false;
142  bool KeepOneInputPHIs = false;
143  bool PreserveLCSSA = false;
145  /// SplitCriticalEdge is guaranteed to preserve loop-simplify form if LI is
146  /// provided. If it cannot be preserved, no splitting will take place. If it
147  /// is not set, preserve loop-simplify form if possible.
148  bool PreserveLoopSimplify = true;
149 
151  LoopInfo *LI = nullptr,
152  MemorySSAUpdater *MSSAU = nullptr,
153  PostDominatorTree *PDT = nullptr)
154  : DT(DT), PDT(PDT), LI(LI), MSSAU(MSSAU) {}
155 
157  MergeIdenticalEdges = true;
158  return *this;
159  }
160 
162  KeepOneInputPHIs = true;
163  return *this;
164  }
165 
167  PreserveLCSSA = true;
168  return *this;
169  }
170 
172  IgnoreUnreachableDests = true;
173  return *this;
174  }
175 
177  PreserveLoopSimplify = false;
178  return *this;
179  }
180 };
181 
182 /// When a loop exit edge is split, LCSSA form may require new PHIs in the new
183 /// exit block. This function inserts the new PHIs, as needed. Preds is a list
184 /// of preds inside the loop, SplitBB is the new loop exit block, and DestBB is
185 /// the old loop exit, now the successor of SplitBB.
186 void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
187  BasicBlock *SplitBB, BasicBlock *DestBB);
188 
189 /// If this edge is a critical edge, insert a new node to split the critical
190 /// edge. This will update the analyses passed in through the option struct.
191 /// This returns the new block if the edge was split, null otherwise.
192 ///
193 /// If MergeIdenticalEdges in the options struct is true (not the default),
194 /// *all* edges from TI to the specified successor will be merged into the same
195 /// critical edge block. This is most commonly interesting with switch
196 /// instructions, which may have many edges to any one destination. This
197 /// ensures that all edges to that dest go to one block instead of each going
198 /// to a different block, but isn't the standard definition of a "critical
199 /// edge".
200 ///
201 /// It is invalid to call this function on a critical edge that starts at an
202 /// IndirectBrInst. Splitting these edges will almost always create an invalid
203 /// program because the address of the new block won't be the one that is jumped
204 /// to.
205 BasicBlock *SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
206  const CriticalEdgeSplittingOptions &Options =
207  CriticalEdgeSplittingOptions(),
208  const Twine &BBName = "");
209 
210 /// If it is known that an edge is critical, SplitKnownCriticalEdge can be
211 /// called directly, rather than calling SplitCriticalEdge first.
212 BasicBlock *SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum,
213  const CriticalEdgeSplittingOptions &Options =
214  CriticalEdgeSplittingOptions(),
215  const Twine &BBName = "");
216 
217 /// If an edge from Src to Dst is critical, split the edge and return true,
218 /// otherwise return false. This method requires that there be an edge between
219 /// the two blocks. It updates the analyses passed in the options struct
220 inline BasicBlock *
224  Instruction *TI = Src->getTerminator();
225  unsigned i = 0;
226  while (true) {
227  assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
228  if (TI->getSuccessor(i) == Dst)
229  return SplitCriticalEdge(TI, i, Options);
230  ++i;
231  }
232 }
233 
234 /// Loop over all of the edges in the CFG, breaking critical edges as they are
235 /// found. Returns the number of broken edges.
236 unsigned SplitAllCriticalEdges(Function &F,
237  const CriticalEdgeSplittingOptions &Options =
238  CriticalEdgeSplittingOptions());
239 
240 /// Split the edge connecting the specified blocks, and return the newly created
241 /// basic block between \p From and \p To.
243  DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
244  MemorySSAUpdater *MSSAU = nullptr,
245  const Twine &BBName = "");
246 
247 /// Sets the unwind edge of an instruction to a particular successor.
248 void setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ);
249 
250 /// Replaces all uses of OldPred with the NewPred block in all PHINodes in a
251 /// block.
252 void updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,
253  BasicBlock *NewPred, PHINode *Until = nullptr);
254 
255 /// Split the edge connect the specficed blocks in the case that \p Succ is an
256 /// Exception Handling Block
258  LandingPadInst *OriginalPad = nullptr,
259  PHINode *LandingPadReplacement = nullptr,
260  const CriticalEdgeSplittingOptions &Options =
261  CriticalEdgeSplittingOptions(),
262  const Twine &BBName = "");
263 
264 /// Split the specified block at the specified instruction.
265 ///
266 /// If \p Before is true, splitBlockBefore handles the block
267 /// splitting. Otherwise, execution proceeds as described below.
268 ///
269 /// Everything before \p SplitPt stays in \p Old and everything starting with \p
270 /// SplitPt moves to a new block. The two blocks are joined by an unconditional
271 /// branch. The new block with name \p BBName is returned.
272 ///
273 /// FIXME: deprecated, switch to the DomTreeUpdater-based one.
274 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT,
275  LoopInfo *LI = nullptr,
276  MemorySSAUpdater *MSSAU = nullptr,
277  const Twine &BBName = "", bool Before = false);
278 
279 /// Split the specified block at the specified instruction.
280 ///
281 /// If \p Before is true, splitBlockBefore handles the block
282 /// splitting. Otherwise, execution proceeds as described below.
283 ///
284 /// Everything before \p SplitPt stays in \p Old and everything starting with \p
285 /// SplitPt moves to a new block. The two blocks are joined by an unconditional
286 /// branch. The new block with name \p BBName is returned.
287 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
288  DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
289  MemorySSAUpdater *MSSAU = nullptr,
290  const Twine &BBName = "", bool Before = false);
291 
292 /// Split the specified block at the specified instruction \p SplitPt.
293 /// All instructions before \p SplitPt are moved to a new block and all
294 /// instructions after \p SplitPt stay in the old block. The new block and the
295 /// old block are joined by inserting an unconditional branch to the end of the
296 /// new block. The new block with name \p BBName is returned.
297 BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
298  DomTreeUpdater *DTU, LoopInfo *LI,
299  MemorySSAUpdater *MSSAU, const Twine &BBName = "");
300 
301 /// This method introduces at least one new basic block into the function and
302 /// moves some of the predecessors of BB to be predecessors of the new block.
303 /// The new predecessors are indicated by the Preds array. The new block is
304 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors
305 /// from Preds are now pointing.
306 ///
307 /// If BB is a landingpad block then additional basicblock might be introduced.
308 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
309 /// details on this case.
310 ///
311 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
312 /// no other analyses. In particular, it does not preserve LoopSimplify
313 /// (because it's complicated to handle the case where one of the edges being
314 /// split is an exit of a loop with other exits).
315 ///
316 /// FIXME: deprecated, switch to the DomTreeUpdater-based one.
317 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
318  const char *Suffix, DominatorTree *DT,
319  LoopInfo *LI = nullptr,
320  MemorySSAUpdater *MSSAU = nullptr,
321  bool PreserveLCSSA = false);
322 
323 /// This method introduces at least one new basic block into the function and
324 /// moves some of the predecessors of BB to be predecessors of the new block.
325 /// The new predecessors are indicated by the Preds array. The new block is
326 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors
327 /// from Preds are now pointing.
328 ///
329 /// If BB is a landingpad block then additional basicblock might be introduced.
330 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
331 /// details on this case.
332 ///
333 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
334 /// no other analyses. In particular, it does not preserve LoopSimplify
335 /// (because it's complicated to handle the case where one of the edges being
336 /// split is an exit of a loop with other exits).
337 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
338  const char *Suffix,
339  DomTreeUpdater *DTU = nullptr,
340  LoopInfo *LI = nullptr,
341  MemorySSAUpdater *MSSAU = nullptr,
342  bool PreserveLCSSA = false);
343 
344 /// This method transforms the landing pad, OrigBB, by introducing two new basic
345 /// blocks into the function. One of those new basic blocks gets the
346 /// predecessors listed in Preds. The other basic block gets the remaining
347 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both
348 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and
349 /// 'Suffix2', and are returned in the NewBBs vector.
350 ///
351 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
352 /// no other analyses. In particular, it does not preserve LoopSimplify
353 /// (because it's complicated to handle the case where one of the edges being
354 /// split is an exit of a loop with other exits).
355 ///
356 /// FIXME: deprecated, switch to the DomTreeUpdater-based one.
358  ArrayRef<BasicBlock *> Preds,
359  const char *Suffix, const char *Suffix2,
360  SmallVectorImpl<BasicBlock *> &NewBBs,
361  DominatorTree *DT, LoopInfo *LI = nullptr,
362  MemorySSAUpdater *MSSAU = nullptr,
363  bool PreserveLCSSA = false);
364 
365 /// This method transforms the landing pad, OrigBB, by introducing two new basic
366 /// blocks into the function. One of those new basic blocks gets the
367 /// predecessors listed in Preds. The other basic block gets the remaining
368 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both
369 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and
370 /// 'Suffix2', and are returned in the NewBBs vector.
371 ///
372 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
373 /// no other analyses. In particular, it does not preserve LoopSimplify
374 /// (because it's complicated to handle the case where one of the edges being
375 /// split is an exit of a loop with other exits).
377  BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix,
378  const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
379  DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr,
380  MemorySSAUpdater *MSSAU = nullptr, bool PreserveLCSSA = false);
381 
382 /// This method duplicates the specified return instruction into a predecessor
383 /// which ends in an unconditional branch. If the return instruction returns a
384 /// value defined by a PHI, propagate the right value into the return. It
385 /// returns the new return instruction in the predecessor.
386 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
387  BasicBlock *Pred,
388  DomTreeUpdater *DTU = nullptr);
389 
390 /// Split the containing block at the specified instruction - everything before
391 /// SplitBefore stays in the old basic block, and the rest of the instructions
392 /// in the BB are moved to a new block. The two blocks are connected by a
393 /// conditional branch (with value of Cmp being the condition).
394 /// Before:
395 /// Head
396 /// SplitBefore
397 /// Tail
398 /// After:
399 /// Head
400 /// if (Cond)
401 /// ThenBlock
402 /// SplitBefore
403 /// Tail
404 ///
405 /// If \p ThenBlock is not specified, a new block will be created for it.
406 /// If \p Unreachable is true, the newly created block will end with
407 /// UnreachableInst, otherwise it branches to Tail.
408 /// Returns the NewBasicBlock's terminator.
409 ///
410 /// Updates DT and LI if given.
411 ///
412 /// FIXME: deprecated, switch to the DomTreeUpdater-based one.
413 Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
414  bool Unreachable, MDNode *BranchWeights,
415  DominatorTree *DT,
416  LoopInfo *LI = nullptr,
417  BasicBlock *ThenBlock = nullptr);
418 
419 /// Split the containing block at the specified instruction - everything before
420 /// SplitBefore stays in the old basic block, and the rest of the instructions
421 /// in the BB are moved to a new block. The two blocks are connected by a
422 /// conditional branch (with value of Cmp being the condition).
423 /// Before:
424 /// Head
425 /// SplitBefore
426 /// Tail
427 /// After:
428 /// Head
429 /// if (Cond)
430 /// ThenBlock
431 /// SplitBefore
432 /// Tail
433 ///
434 /// If \p ThenBlock is not specified, a new block will be created for it.
435 /// If \p Unreachable is true, the newly created block will end with
436 /// UnreachableInst, otherwise it branches to Tail.
437 /// Returns the NewBasicBlock's terminator.
438 ///
439 /// Updates DT and LI if given.
440 Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
441  bool Unreachable,
442  MDNode *BranchWeights = nullptr,
443  DomTreeUpdater *DTU = nullptr,
444  LoopInfo *LI = nullptr,
445  BasicBlock *ThenBlock = nullptr);
446 
447 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
448 /// but also creates the ElseBlock.
449 /// Before:
450 /// Head
451 /// SplitBefore
452 /// Tail
453 /// After:
454 /// Head
455 /// if (Cond)
456 /// ThenBlock
457 /// else
458 /// ElseBlock
459 /// SplitBefore
460 /// Tail
461 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
462  Instruction **ThenTerm,
463  Instruction **ElseTerm,
464  MDNode *BranchWeights = nullptr);
465 
466 /// Check whether BB is the merge point of a if-region.
467 /// If so, return the branch instruction that determines which entry into
468 /// BB will be taken. Also, return by references the block that will be
469 /// entered from if the condition is true, and the block that will be
470 /// entered if the condition is false.
471 ///
472 /// This does no checking to see if the true/false blocks have large or unsavory
473 /// instructions in them.
474 BranchInst *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
475  BasicBlock *&IfFalse);
476 
477 // Split critical edges where the source of the edge is an indirectbr
478 // instruction. This isn't always possible, but we can handle some easy cases.
479 // This is useful because MI is unable to split such critical edges,
480 // which means it will not be able to sink instructions along those edges.
481 // This is especially painful for indirect branches with many successors, where
482 // we end up having to prepare all outgoing values in the origin block.
483 //
484 // Our normal algorithm for splitting critical edges requires us to update
485 // the outgoing edges of the edge origin block, but for an indirectbr this
486 // is hard, since it would require finding and updating the block addresses
487 // the indirect branch uses. But if a block only has a single indirectbr
488 // predecessor, with the others being regular branches, we can do it in a
489 // different way.
490 // Say we have A -> D, B -> D, I -> D where only I -> D is an indirectbr.
491 // We can split D into D0 and D1, where D0 contains only the PHIs from D,
492 // and D1 is the D block body. We can then duplicate D0 as D0A and D0B, and
493 // create the following structure:
494 // A -> D0A, B -> D0A, I -> D0B, D0A -> D1, D0B -> D1
495 // If BPI and BFI aren't non-null, BPI/BFI will be updated accordingly.
496 bool SplitIndirectBrCriticalEdges(Function &F,
497  BranchProbabilityInfo *BPI = nullptr,
498  BlockFrequencyInfo *BFI = nullptr);
499 
500 /// Given a set of incoming and outgoing blocks, create a "hub" such that every
501 /// edge from an incoming block InBB to an outgoing block OutBB is now split
502 /// into two edges, one from InBB to the hub and another from the hub to
503 /// OutBB. The hub consists of a series of guard blocks, one for each outgoing
504 /// block. Each guard block conditionally branches to the corresponding outgoing
505 /// block, or the next guard block in the chain. These guard blocks are returned
506 /// in the argument vector.
507 ///
508 /// Since the control flow edges from InBB to OutBB have now been replaced, the
509 /// function also updates any PHINodes in OutBB. For each such PHINode, the
510 /// operands corresponding to incoming blocks are moved to a new PHINode in the
511 /// hub, and the hub is made an operand of the original PHINode.
512 ///
513 /// Input CFG:
514 /// ----------
515 ///
516 /// Def
517 /// |
518 /// v
519 /// In1 In2
520 /// | |
521 /// | |
522 /// v v
523 /// Foo ---> Out1 Out2
524 /// |
525 /// v
526 /// Use
527 ///
528 ///
529 /// Create hub: Incoming = {In1, In2}, Outgoing = {Out1, Out2}
530 /// ----------------------------------------------------------
531 ///
532 /// Def
533 /// |
534 /// v
535 /// In1 In2 Foo
536 /// | Hub | |
537 /// | + - - | - - + |
538 /// | ' v ' V
539 /// +------> Guard1 -----> Out1
540 /// ' | '
541 /// ' v '
542 /// ' Guard2 -----> Out2
543 /// ' ' |
544 /// + - - - - - + |
545 /// v
546 /// Use
547 ///
548 /// Limitations:
549 /// -----------
550 /// 1. This assumes that all terminators in the CFG are direct branches (the
551 /// "br" instruction). The presence of any other control flow such as
552 /// indirectbr, switch or callbr will cause an assert.
553 ///
554 /// 2. The updates to the PHINodes are not sufficient to restore SSA
555 /// form. Consider a definition Def, its use Use, incoming block In2 and
556 /// outgoing block Out2, such that:
557 /// a. In2 is reachable from D or contains D.
558 /// b. U is reachable from Out2 or is contained in Out2.
559 /// c. U is not a PHINode if U is contained in Out2.
560 ///
561 /// Clearly, Def dominates Out2 since the program is valid SSA. But when the
562 /// hub is introduced, there is a new path through the hub along which Use is
563 /// reachable from entry without passing through Def, and SSA is no longer
564 /// valid. To fix this, we need to look at all the blocks post-dominated by
565 /// the hub on the one hand, and dominated by Out2 on the other. This is left
566 /// for the caller to accomplish, since each specific use of this function
567 /// may have additional information which simplifies this fixup. For example,
568 /// see restoreSSA() in the UnifyLoopExits pass.
569 BasicBlock *CreateControlFlowHub(DomTreeUpdater *DTU,
570  SmallVectorImpl<BasicBlock *> &GuardBlocks,
571  const SetVector<BasicBlock *> &Predecessors,
572  const SetVector<BasicBlock *> &Successors,
573  const StringRef Prefix);
574 
575 } // end namespace llvm
576 
577 #endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
i
i
Definition: README.txt:29
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::SplitLandingPadPredecessors
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
Definition: BasicBlockUtils.cpp:1268
llvm::CriticalEdgeSplittingOptions::MSSAU
MemorySSAUpdater * MSSAU
Definition: BasicBlockUtils.h:140
llvm::BasicBlock::InstListType
SymbolTableList< Instruction > InstListType
Definition: BasicBlock.h:61
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:90
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
DomTreeUpdater.h
llvm::DeleteDeadBlocks
void DeleteDeadBlocks(ArrayRef< BasicBlock * > BBs, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified blocks from BB.
Definition: BasicBlockUtils.cpp:94
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
llvm::createPHIsForSplitLoopExit
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.
Definition: BasicBlockUtils.cpp:712
llvm::SplitCriticalEdge
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.
Definition: BreakCriticalEdges.cpp:103
llvm::FoldReturnIntoUncondBranch
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...
Definition: BasicBlockUtils.cpp:1291
llvm::CriticalEdgeSplittingOptions::LI
LoopInfo * LI
Definition: BasicBlockUtils.h:139
llvm::CriticalEdgeSplittingOptions::IgnoreUnreachableDests
bool IgnoreUnreachableDests
Definition: BasicBlockUtils.h:144
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::Instruction::getNumSuccessors
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Definition: Instruction.cpp:765
llvm::CriticalEdgeSplittingOptions::setKeepOneInputPHIs
CriticalEdgeSplittingOptions & setKeepOneInputPHIs()
Definition: BasicBlockUtils.h:161
llvm::CreateControlFlowHub
BasicBlock * CreateControlFlowHub(DomTreeUpdater *DTU, SmallVectorImpl< BasicBlock * > &GuardBlocks, const SetVector< BasicBlock * > &Predecessors, const SetVector< BasicBlock * > &Successors, const StringRef Prefix)
Given a set of incoming and outgoing blocks, create a "hub" such that every edge from an incoming blo...
InstrTypes.h
llvm::SplitKnownCriticalEdge
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,...
Definition: BreakCriticalEdges.cpp:113
llvm::ReplaceInstWithInst
void ReplaceInstWithInst(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
Definition: BasicBlockUtils.cpp:468
llvm::CriticalEdgeSplittingOptions
Option class for critical edge splitting.
Definition: BasicBlockUtils.h:136
llvm::Instruction
Definition: Instruction.h:45
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::Instruction::getSuccessor
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
Definition: Instruction.cpp:777
llvm::SplitBlockPredecessors
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...
Definition: BasicBlockUtils.cpp:1148
llvm::RemoveRedundantDbgInstrs
bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
Definition: BasicBlockUtils.cpp:432
llvm::ehAwareSplitEdge
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.
Definition: BasicBlockUtils.cpp:564
llvm::EliminateUnreachableBlocks
bool EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete all basic blocks from F that are not reachable from its entry node.
Definition: BasicBlockUtils.cpp:118
CFG.h
LoopInfo.h
llvm::GetIfCondition
BranchInst * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
Check whether BB is the merge point of a if-region.
Definition: BasicBlockUtils.cpp:1459
BasicBlock.h
llvm::CriticalEdgeSplittingOptions::KeepOneInputPHIs
bool KeepOneInputPHIs
Definition: BasicBlockUtils.h:142
llvm::CriticalEdgeSplittingOptions::CriticalEdgeSplittingOptions
CriticalEdgeSplittingOptions(DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, PostDominatorTree *PDT=nullptr)
Definition: BasicBlockUtils.h:150
llvm::MemorySSAUpdater
Definition: MemorySSAUpdater.h:56
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::CriticalEdgeSplittingOptions::setMergeIdenticalEdges
CriticalEdgeSplittingOptions & setMergeIdenticalEdges()
Definition: BasicBlockUtils.h:156
llvm::CriticalEdgeSplittingOptions::unsetPreserveLoopSimplify
CriticalEdgeSplittingOptions & unsetPreserveLoopSimplify()
Definition: BasicBlockUtils.h:176
llvm::SplitIndirectBrCriticalEdges
bool SplitIndirectBrCriticalEdges(Function &F, BranchProbabilityInfo *BPI=nullptr, BlockFrequencyInfo *BFI=nullptr)
Definition: BreakCriticalEdges.cpp:351
llvm::updatePhiNodes
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.
Definition: BasicBlockUtils.cpp:542
ArrayRef.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::CriticalEdgeSplittingOptions::PreserveLCSSA
bool PreserveLCSSA
Definition: BasicBlockUtils.h:143
llvm::ISD::BasicBlock
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
llvm::SplitBlockAndInsertIfThenElse
void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
Definition: BasicBlockUtils.cpp:1439
llvm::CriticalEdgeSplittingOptions::PDT
PostDominatorTree * PDT
Definition: BasicBlockUtils.h:138
llvm::ReplayInlineScope::Function
@ Function
llvm::LoopInfo
Definition: LoopInfo.h:1083
llvm::MergeBlockIntoPredecessor
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false)
Attempts to merge a block into its predecessor, if possible.
Definition: BasicBlockUtils.cpp:173
Cond
SmallVector< MachineOperand, 4 > Cond
Definition: BasicBlockSections.cpp:179
llvm::PostDominatorTree
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
Definition: PostDominators.h:28
llvm::DetatchDeadBlocks
void DetatchDeadBlocks(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< DominatorTree::UpdateType > *Updates, bool KeepOneInputPHIs=false)
Replace contents of every block in BBs with single unreachable instruction.
Definition: BasicBlockUtils.cpp:55
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:421
llvm::CriticalEdgeSplittingOptions::MergeIdenticalEdges
bool MergeIdenticalEdges
Definition: BasicBlockUtils.h:141
llvm::ReplaceInstWithValue
void ReplaceInstWithValue(BasicBlock::InstListType &BIL, BasicBlock::iterator &BI, Value *V)
Replace all uses of an instruction (specified by BI) with a value, then remove and delete the origina...
Definition: BasicBlockUtils.cpp:454
llvm::SplitEdge
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...
Definition: BasicBlockUtils.cpp:493
llvm::setUnwindEdgeTo
void setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ)
Sets the unwind edge of an instruction to a particular successor.
Definition: BasicBlockUtils.cpp:531
llvm::CriticalEdgeSplittingOptions::setPreserveLCSSA
CriticalEdgeSplittingOptions & setPreserveLCSSA()
Definition: BasicBlockUtils.h:166
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::splitBlockBefore
BasicBlock * splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, DomTreeUpdater *DTU, LoopInfo *LI, MemorySSAUpdater *MSSAU, const Twine &BBName="")
Split the specified block at the specified instruction SplitPt.
Definition: BasicBlockUtils.cpp:829
llvm::FoldSingleEntryPHINodes
bool FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
Definition: BasicBlockUtils.cpp:138
llvm::DeleteDeadPHIs
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.
Definition: BasicBlockUtils.cpp:157
llvm::CriticalEdgeSplittingOptions::DT
DominatorTree * DT
Definition: BasicBlockUtils.h:137
llvm::SplitAllCriticalEdges
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
Loop over all of the edges in the CFG, breaking critical edges as they are found.
Definition: BasicBlockUtils.cpp:744
llvm::MergeBlockSuccessorsIntoGivenBlocks
bool MergeBlockSuccessorsIntoGivenBlocks(SmallPtrSetImpl< BasicBlock * > &MergeBlocks, Loop *L=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr)
Merge block(s) sucessors, if possible.
Definition: BasicBlockUtils.cpp:310
llvm::DeleteDeadBlock
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
Definition: BasicBlockUtils.cpp:89
llvm::CriticalEdgeSplittingOptions::setIgnoreUnreachableDests
CriticalEdgeSplittingOptions & setIgnoreUnreachableDests()
Definition: BasicBlockUtils.h:171
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::SplitBlockAndInsertIfThen
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
Definition: BasicBlockUtils.cpp:1418
llvm::SplitBlock
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
Definition: BasicBlockUtils.cpp:814
llvm::CriticalEdgeSplittingOptions::PreserveLoopSimplify
bool PreserveLoopSimplify
SplitCriticalEdge is guaranteed to preserve loop-simplify form if LI is provided.
Definition: BasicBlockUtils.h:148
SetVector.h