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/Analysis/DomTreeUpdater.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.
77 void FoldSingleEntryPHINodes(BasicBlock *BB,
78                              MemoryDependenceResults *MemDep = nullptr);
79 
80 /// Examine each PHI in the given block and delete it if it is dead. Also
81 /// recursively delete any operands that become dead as a result. This includes
82 /// tracing the def-use list from the PHI to see if it is ultimately unused or
83 /// if it reaches an unused cycle. Return true if any PHIs were deleted.
84 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr,
85                     MemorySSAUpdater *MSSAU = nullptr);
86 
87 /// Attempts to merge a block into its predecessor, if possible. The return
88 /// value indicates success or failure.
89 /// By default do not merge blocks if BB's predecessor has multiple successors.
90 /// If PredecessorWithTwoSuccessors = true, the blocks can only be merged
91 /// if BB's Pred has a branch to BB and to AnotherBB, and BB has a single
92 /// successor Sing. In this case the branch will be updated with Sing instead of
93 /// BB, and BB will still be merged into its predecessor and removed.
94 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.
107 bool MergeBlockSuccessorsIntoGivenBlocks(
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.
113 bool RemoveRedundantDbgInstrs(BasicBlock *BB);
114 
115 /// Replace all uses of an instruction (specified by BI) with a value, then
116 /// remove and delete the original instruction.
117 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
118                           BasicBlock::iterator &BI, Value *V);
119 
120 /// Replace the instruction specified by BI with the instruction specified by I.
121 /// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The
122 /// original instruction is deleted and BI is updated to point to the new
123 /// instruction.
124 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
125                          BasicBlock::iterator &BI, Instruction *I);
126 
127 /// Replace the instruction specified by From with the instruction specified by
128 /// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc.
129 void ReplaceInstWithInst(Instruction *From, Instruction *To);
130 
131 /// Option class for critical edge splitting.
132 ///
133 /// This provides a builder interface for overriding the default options used
134 /// during critical edge splitting.
135 struct CriticalEdgeSplittingOptions {
136   DominatorTree *DT;
137   PostDominatorTree *PDT;
138   LoopInfo *LI;
139   MemorySSAUpdater *MSSAU;
140   bool MergeIdenticalEdges = false;
141   bool KeepOneInputPHIs = false;
142   bool PreserveLCSSA = false;
143   bool IgnoreUnreachableDests = false;
144   /// SplitCriticalEdge is guaranteed to preserve loop-simplify form if LI is
145   /// provided. If it cannot be preserved, no splitting will take place. If it
146   /// is not set, preserve loop-simplify form if possible.
147   bool PreserveLoopSimplify = true;
148 
149   CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr,
150                                LoopInfo *LI = nullptr,
151                                MemorySSAUpdater *MSSAU = nullptr,
152                                PostDominatorTree *PDT = nullptr)
153       : DT(DT), PDT(PDT), LI(LI), MSSAU(MSSAU) {}
154 
155   CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
156     MergeIdenticalEdges = true;
157     return *this;
158   }
159 
160   CriticalEdgeSplittingOptions &setKeepOneInputPHIs() {
161     KeepOneInputPHIs = true;
162     return *this;
163   }
164 
165   CriticalEdgeSplittingOptions &setPreserveLCSSA() {
166     PreserveLCSSA = true;
167     return *this;
168   }
169 
170   CriticalEdgeSplittingOptions &setIgnoreUnreachableDests() {
171     IgnoreUnreachableDests = true;
172     return *this;
173   }
174 
175   CriticalEdgeSplittingOptions &unsetPreserveLoopSimplify() {
176     PreserveLoopSimplify = false;
177     return *this;
178   }
179 };
180 
181 /// If this edge is a critical edge, insert a new node to split the critical
182 /// edge. This will update the analyses passed in through the option struct.
183 /// This returns the new block if the edge was split, null otherwise.
184 ///
185 /// If MergeIdenticalEdges in the options struct is true (not the default),
186 /// *all* edges from TI to the specified successor will be merged into the same
187 /// critical edge block. This is most commonly interesting with switch
188 /// instructions, which may have many edges to any one destination.  This
189 /// ensures that all edges to that dest go to one block instead of each going
190 /// to a different block, but isn't the standard definition of a "critical
191 /// edge".
192 ///
193 /// It is invalid to call this function on a critical edge that starts at an
194 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
195 /// program because the address of the new block won't be the one that is jumped
196 /// to.
197 BasicBlock *SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
198                               const CriticalEdgeSplittingOptions &Options =
199                                   CriticalEdgeSplittingOptions());
200 
201 inline BasicBlock *
202 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
203                   const CriticalEdgeSplittingOptions &Options =
204                       CriticalEdgeSplittingOptions()) {
205   return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
206                            Options);
207 }
208 
209 /// If the edge from *PI to BB is not critical, return false. Otherwise, split
210 /// all edges between the two blocks and return true. This updates all of the
211 /// same analyses as the other SplitCriticalEdge function. If P is specified, it
212 /// updates the analyses described above.
213 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
214                               const CriticalEdgeSplittingOptions &Options =
215                                   CriticalEdgeSplittingOptions()) {
216   bool MadeChange = false;
217   Instruction *TI = (*PI)->getTerminator();
218   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
219     if (TI->getSuccessor(i) == Succ)
220       MadeChange |= !!SplitCriticalEdge(TI, i, Options);
221   return MadeChange;
222 }
223 
224 /// If an edge from Src to Dst is critical, split the edge and return true,
225 /// otherwise return false. This method requires that there be an edge between
226 /// the two blocks. It updates the analyses passed in the options struct
227 inline BasicBlock *
228 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
229                   const CriticalEdgeSplittingOptions &Options =
230                       CriticalEdgeSplittingOptions()) {
231   Instruction *TI = Src->getTerminator();
232   unsigned i = 0;
233   while (true) {
234     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
235     if (TI->getSuccessor(i) == Dst)
236       return SplitCriticalEdge(TI, i, Options);
237     ++i;
238   }
239 }
240 
241 /// Loop over all of the edges in the CFG, breaking critical edges as they are
242 /// found. Returns the number of broken edges.
243 unsigned SplitAllCriticalEdges(Function &F,
244                                const CriticalEdgeSplittingOptions &Options =
245                                    CriticalEdgeSplittingOptions());
246 
247 /// Split the edge connecting specified block.
248 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
249                       DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
250                       MemorySSAUpdater *MSSAU = nullptr);
251 
252 /// Split the specified block at the specified instruction - everything before
253 /// SplitPt stays in Old and everything starting with SplitPt moves to a new
254 /// block. The two blocks are joined by an unconditional branch and the loop
255 /// info is updated.
256 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
257                        DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
258                        MemorySSAUpdater *MSSAU = nullptr,
259                        const Twine &BBName = "");
260 
261 /// This method introduces at least one new basic block into the function and
262 /// moves some of the predecessors of BB to be predecessors of the new block.
263 /// The new predecessors are indicated by the Preds array. The new block is
264 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors
265 /// from Preds are now pointing.
266 ///
267 /// If BB is a landingpad block then additional basicblock might be introduced.
268 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
269 /// details on this case.
270 ///
271 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
272 /// no other analyses. In particular, it does not preserve LoopSimplify
273 /// (because it's complicated to handle the case where one of the edges being
274 /// split is an exit of a loop with other exits).
275 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
276                                    const char *Suffix,
277                                    DominatorTree *DT = nullptr,
278                                    LoopInfo *LI = nullptr,
279                                    MemorySSAUpdater *MSSAU = nullptr,
280                                    bool PreserveLCSSA = false);
281 
282 /// This method transforms the landing pad, OrigBB, by introducing two new basic
283 /// blocks into the function. One of those new basic blocks gets the
284 /// predecessors listed in Preds. The other basic block gets the remaining
285 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both
286 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and
287 /// 'Suffix2', and are returned in the NewBBs vector.
288 ///
289 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
290 /// no other analyses. In particular, it does not preserve LoopSimplify
291 /// (because it's complicated to handle the case where one of the edges being
292 /// split is an exit of a loop with other exits).
293 void SplitLandingPadPredecessors(
294     BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix,
295     const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
296     DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
297     MemorySSAUpdater *MSSAU = nullptr, bool PreserveLCSSA = false);
298 
299 /// This method duplicates the specified return instruction into a predecessor
300 /// which ends in an unconditional branch. If the return instruction returns a
301 /// value defined by a PHI, propagate the right value into the return. It
302 /// returns the new return instruction in the predecessor.
303 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
304                                        BasicBlock *Pred,
305                                        DomTreeUpdater *DTU = nullptr);
306 
307 /// Split the containing block at the specified instruction - everything before
308 /// SplitBefore stays in the old basic block, and the rest of the instructions
309 /// in the BB are moved to a new block. The two blocks are connected by a
310 /// conditional branch (with value of Cmp being the condition).
311 /// Before:
312 ///   Head
313 ///   SplitBefore
314 ///   Tail
315 /// After:
316 ///   Head
317 ///   if (Cond)
318 ///     ThenBlock
319 ///   SplitBefore
320 ///   Tail
321 ///
322 /// If \p ThenBlock is not specified, a new block will be created for it.
323 /// If \p Unreachable is true, the newly created block will end with
324 /// UnreachableInst, otherwise it branches to Tail.
325 /// Returns the NewBasicBlock's terminator.
326 ///
327 /// Updates DT and LI if given.
328 Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
329                                        bool Unreachable,
330                                        MDNode *BranchWeights = nullptr,
331                                        DominatorTree *DT = nullptr,
332                                        LoopInfo *LI = nullptr,
333                                        BasicBlock *ThenBlock = nullptr);
334 
335 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
336 /// but also creates the ElseBlock.
337 /// Before:
338 ///   Head
339 ///   SplitBefore
340 ///   Tail
341 /// After:
342 ///   Head
343 ///   if (Cond)
344 ///     ThenBlock
345 ///   else
346 ///     ElseBlock
347 ///   SplitBefore
348 ///   Tail
349 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
350                                    Instruction **ThenTerm,
351                                    Instruction **ElseTerm,
352                                    MDNode *BranchWeights = nullptr);
353 
354 /// Check whether BB is the merge point of a if-region.
355 /// If so, return the boolean condition that determines which entry into
356 /// BB will be taken.  Also, return by references the block that will be
357 /// entered from if the condition is true, and the block that will be
358 /// entered if the condition is false.
359 ///
360 /// This does no checking to see if the true/false blocks have large or unsavory
361 /// instructions in them.
362 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
363                       BasicBlock *&IfFalse);
364 
365 // Split critical edges where the source of the edge is an indirectbr
366 // instruction. This isn't always possible, but we can handle some easy cases.
367 // This is useful because MI is unable to split such critical edges,
368 // which means it will not be able to sink instructions along those edges.
369 // This is especially painful for indirect branches with many successors, where
370 // we end up having to prepare all outgoing values in the origin block.
371 //
372 // Our normal algorithm for splitting critical edges requires us to update
373 // the outgoing edges of the edge origin block, but for an indirectbr this
374 // is hard, since it would require finding and updating the block addresses
375 // the indirect branch uses. But if a block only has a single indirectbr
376 // predecessor, with the others being regular branches, we can do it in a
377 // different way.
378 // Say we have A -> D, B -> D, I -> D where only I -> D is an indirectbr.
379 // We can split D into D0 and D1, where D0 contains only the PHIs from D,
380 // and D1 is the D block body. We can then duplicate D0 as D0A and D0B, and
381 // create the following structure:
382 // A -> D0A, B -> D0A, I -> D0B, D0A -> D1, D0B -> D1
383 // If BPI and BFI aren't non-null, BPI/BFI will be updated accordingly.
384 bool SplitIndirectBrCriticalEdges(Function &F,
385                                   BranchProbabilityInfo *BPI = nullptr,
386                                   BlockFrequencyInfo *BFI = nullptr);
387 
388 /// Given a set of incoming and outgoing blocks, create a "hub" such that every
389 /// edge from an incoming block InBB to an outgoing block OutBB is now split
390 /// into two edges, one from InBB to the hub and another from the hub to
391 /// OutBB. The hub consists of a series of guard blocks, one for each outgoing
392 /// block. Each guard block conditionally branches to the corresponding outgoing
393 /// block, or the next guard block in the chain. These guard blocks are returned
394 /// in the argument vector.
395 ///
396 /// Since the control flow edges from InBB to OutBB have now been replaced, the
397 /// function also updates any PHINodes in OutBB. For each such PHINode, the
398 /// operands corresponding to incoming blocks are moved to a new PHINode in the
399 /// hub, and the hub is made an operand of the original PHINode.
400 ///
401 /// Input CFG:
402 /// ----------
403 ///
404 ///                    Def
405 ///                     |
406 ///                     v
407 ///           In1      In2
408 ///            |        |
409 ///            |        |
410 ///            v        v
411 ///  Foo ---> Out1     Out2
412 ///                     |
413 ///                     v
414 ///                    Use
415 ///
416 ///
417 /// Create hub: Incoming = {In1, In2}, Outgoing = {Out1, Out2}
418 /// ----------------------------------------------------------
419 ///
420 ///             Def
421 ///              |
422 ///              v
423 ///  In1        In2          Foo
424 ///   |    Hub   |            |
425 ///   |    + - - | - - +      |
426 ///   |    '     v     '      V
427 ///   +------> Guard1 -----> Out1
428 ///        '     |     '
429 ///        '     v     '
430 ///        '   Guard2 -----> Out2
431 ///        '           '      |
432 ///        + - - - - - +      |
433 ///                           v
434 ///                          Use
435 ///
436 /// Limitations:
437 /// -----------
438 /// 1. This assumes that all terminators in the CFG are direct branches (the
439 ///    "br" instruction). The presence of any other control flow such as
440 ///    indirectbr, switch or callbr will cause an assert.
441 ///
442 /// 2. The updates to the PHINodes are not sufficient to restore SSA
443 ///    form. Consider a definition Def, its use Use, incoming block In2 and
444 ///    outgoing block Out2, such that:
445 ///    a. In2 is reachable from D or contains D.
446 ///    b. U is reachable from Out2 or is contained in Out2.
447 ///    c. U is not a PHINode if U is contained in Out2.
448 ///
449 ///    Clearly, Def dominates Out2 since the program is valid SSA. But when the
450 ///    hub is introduced, there is a new path through the hub along which Use is
451 ///    reachable from entry without passing through Def, and SSA is no longer
452 ///    valid. To fix this, we need to look at all the blocks post-dominated by
453 ///    the hub on the one hand, and dominated by Out2 on the other. This is left
454 ///    for the caller to accomplish, since each specific use of this function
455 ///    may have additional information which simplifies this fixup. For example,
456 ///    see restoreSSA() in the UnifyLoopExits pass.
457 BasicBlock *CreateControlFlowHub(DomTreeUpdater *DTU,
458                                  SmallVectorImpl<BasicBlock *> &GuardBlocks,
459                                  const SetVector<BasicBlock *> &Predecessors,
460                                  const SetVector<BasicBlock *> &Successors,
461                                  const StringRef Prefix);
462 
463 } // end namespace llvm
464 
465 #endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
466