BasicBlockUtils.h revision 327952
1327952Sdim//===- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils -----*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This family of functions perform manipulations on basic blocks, and
11193323Sed// instructions contained within basic blocks.
12193323Sed//
13193323Sed//===----------------------------------------------------------------------===//
14193323Sed
15249423Sdim#ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
16249423Sdim#define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
17193323Sed
18193323Sed// FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
19193323Sed
20314564Sdim#include "llvm/ADT/ArrayRef.h"
21249423Sdim#include "llvm/IR/BasicBlock.h"
22276479Sdim#include "llvm/IR/CFG.h"
23314564Sdim#include "llvm/IR/InstrTypes.h"
24314564Sdim#include <cassert>
25193323Sed
26193323Sednamespace llvm {
27193323Sed
28327952Sdimclass BlockFrequencyInfo;
29327952Sdimclass BranchProbabilityInfo;
30276479Sdimclass DominatorTree;
31327952Sdimclass Function;
32327952Sdimclass Instruction;
33288943Sdimclass LoopInfo;
34243830Sdimclass MDNode;
35327952Sdimclass MemoryDependenceResults;
36218893Sdimclass ReturnInst;
37243830Sdimclass TargetLibraryInfo;
38327952Sdimclass Value;
39193323Sed
40309124Sdim/// Delete the specified block, which must have no predecessors.
41193323Sedvoid DeleteDeadBlock(BasicBlock *BB);
42226633Sdim
43309124Sdim/// We know that BB has one predecessor. If there are any single-entry PHI nodes
44309124Sdim/// in it, fold them away. This handles the case when all entries to the PHI
45309124Sdim/// nodes in a block are guaranteed equal, such as when the block has exactly
46309124Sdim/// one predecessor.
47296417Sdimvoid FoldSingleEntryPHINodes(BasicBlock *BB,
48309124Sdim                             MemoryDependenceResults *MemDep = nullptr);
49193323Sed
50309124Sdim/// Examine each PHI in the given block and delete it if it is dead. Also
51309124Sdim/// recursively delete any operands that become dead as a result. This includes
52309124Sdim/// tracing the def-use list from the PHI to see if it is ultimately unused or
53309124Sdim/// if it reaches an unused cycle. Return true if any PHIs were deleted.
54276479Sdimbool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr);
55193323Sed
56309124Sdim/// Attempts to merge a block into its predecessor, if possible. The return
57309124Sdim/// value indicates success or failure.
58288943Sdimbool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT = nullptr,
59288943Sdim                               LoopInfo *LI = nullptr,
60309124Sdim                               MemoryDependenceResults *MemDep = nullptr);
61193323Sed
62309124Sdim/// Replace all uses of an instruction (specified by BI) with a value, then
63309124Sdim/// remove and delete the original instruction.
64193323Sedvoid ReplaceInstWithValue(BasicBlock::InstListType &BIL,
65193323Sed                          BasicBlock::iterator &BI, Value *V);
66193323Sed
67309124Sdim/// Replace the instruction specified by BI with the instruction specified by I.
68309124Sdim/// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The
69309124Sdim/// original instruction is deleted and BI is updated to point to the new
70309124Sdim/// instruction.
71193323Sedvoid ReplaceInstWithInst(BasicBlock::InstListType &BIL,
72193323Sed                         BasicBlock::iterator &BI, Instruction *I);
73193323Sed
74309124Sdim/// Replace the instruction specified by From with the instruction specified by
75309124Sdim/// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc.
76193323Sedvoid ReplaceInstWithInst(Instruction *From, Instruction *To);
77193323Sed
78309124Sdim/// Option class for critical edge splitting.
79288943Sdim///
80288943Sdim/// This provides a builder interface for overriding the default options used
81288943Sdim/// during critical edge splitting.
82288943Sdimstruct CriticalEdgeSplittingOptions {
83288943Sdim  DominatorTree *DT;
84288943Sdim  LoopInfo *LI;
85321369Sdim  bool MergeIdenticalEdges = false;
86321369Sdim  bool DontDeleteUselessPHIs = false;
87321369Sdim  bool PreserveLCSSA = false;
88288943Sdim
89296417Sdim  CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr,
90288943Sdim                               LoopInfo *LI = nullptr)
91321369Sdim      : DT(DT), LI(LI) {}
92288943Sdim
93288943Sdim  CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
94288943Sdim    MergeIdenticalEdges = true;
95288943Sdim    return *this;
96288943Sdim  }
97288943Sdim
98288943Sdim  CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
99288943Sdim    DontDeleteUselessPHIs = true;
100288943Sdim    return *this;
101288943Sdim  }
102288943Sdim
103288943Sdim  CriticalEdgeSplittingOptions &setPreserveLCSSA() {
104288943Sdim    PreserveLCSSA = true;
105288943Sdim    return *this;
106288943Sdim  }
107288943Sdim};
108288943Sdim
109309124Sdim/// If this edge is a critical edge, insert a new node to split the critical
110309124Sdim/// edge. This will update the analyses passed in through the option struct.
111309124Sdim/// This returns the new block if the edge was split, null otherwise.
112193323Sed///
113288943Sdim/// If MergeIdenticalEdges in the options struct is true (not the default),
114288943Sdim/// *all* edges from TI to the specified successor will be merged into the same
115288943Sdim/// critical edge block. This is most commonly interesting with switch
116288943Sdim/// instructions, which may have many edges to any one destination.  This
117288943Sdim/// ensures that all edges to that dest go to one block instead of each going
118288943Sdim/// to a different block, but isn't the standard definition of a "critical
119288943Sdim/// edge".
120193323Sed///
121198892Srdivacky/// It is invalid to call this function on a critical edge that starts at an
122198892Srdivacky/// IndirectBrInst.  Splitting these edges will almost always create an invalid
123198892Srdivacky/// program because the address of the new block won't be the one that is jumped
124198892Srdivacky/// to.
125198090SrdivackyBasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
126288943Sdim                              const CriticalEdgeSplittingOptions &Options =
127288943Sdim                                  CriticalEdgeSplittingOptions());
128193323Sed
129288943Sdiminline BasicBlock *
130288943SdimSplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
131288943Sdim                  const CriticalEdgeSplittingOptions &Options =
132288943Sdim                      CriticalEdgeSplittingOptions()) {
133288943Sdim  return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
134288943Sdim                           Options);
135193323Sed}
136193323Sed
137309124Sdim/// If the edge from *PI to BB is not critical, return false. Otherwise, split
138309124Sdim/// all edges between the two blocks and return true. This updates all of the
139309124Sdim/// same analyses as the other SplitCriticalEdge function. If P is specified, it
140309124Sdim/// updates the analyses described above.
141276479Sdiminline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
142288943Sdim                              const CriticalEdgeSplittingOptions &Options =
143288943Sdim                                  CriticalEdgeSplittingOptions()) {
144193323Sed  bool MadeChange = false;
145193323Sed  TerminatorInst *TI = (*PI)->getTerminator();
146193323Sed  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
147193323Sed    if (TI->getSuccessor(i) == Succ)
148288943Sdim      MadeChange |= !!SplitCriticalEdge(TI, i, Options);
149193323Sed  return MadeChange;
150193323Sed}
151193323Sed
152309124Sdim/// If an edge from Src to Dst is critical, split the edge and return true,
153309124Sdim/// otherwise return false. This method requires that there be an edge between
154309124Sdim/// the two blocks. It updates the analyses passed in the options struct
155288943Sdiminline BasicBlock *
156288943SdimSplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
157288943Sdim                  const CriticalEdgeSplittingOptions &Options =
158288943Sdim                      CriticalEdgeSplittingOptions()) {
159193323Sed  TerminatorInst *TI = Src->getTerminator();
160193323Sed  unsigned i = 0;
161314564Sdim  while (true) {
162193323Sed    assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
163193323Sed    if (TI->getSuccessor(i) == Dst)
164288943Sdim      return SplitCriticalEdge(TI, i, Options);
165193323Sed    ++i;
166193323Sed  }
167193323Sed}
168193323Sed
169309124Sdim/// Loop over all of the edges in the CFG, breaking critical edges as they are
170309124Sdim/// found. Returns the number of broken edges.
171288943Sdimunsigned SplitAllCriticalEdges(Function &F,
172288943Sdim                               const CriticalEdgeSplittingOptions &Options =
173288943Sdim                                   CriticalEdgeSplittingOptions());
174280031Sdim
175309124Sdim/// Split the edge connecting specified block.
176288943SdimBasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
177288943Sdim                      DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
178193323Sed
179309124Sdim/// Split the specified block at the specified instruction - everything before
180309124Sdim/// SplitPt stays in Old and everything starting with SplitPt moves to a new
181309124Sdim/// block. The two blocks are joined by an unconditional branch and the loop
182309124Sdim/// info is updated.
183288943SdimBasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
184288943Sdim                       DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
185226633Sdim
186309124Sdim/// This method introduces at least one new basic block into the function and
187309124Sdim/// moves some of the predecessors of BB to be predecessors of the new block.
188309124Sdim/// The new predecessors are indicated by the Preds array. The new block is
189309124Sdim/// given a suffix of 'Suffix'. Returns new basic block to which predecessors
190309124Sdim/// from Preds are now pointing.
191193323Sed///
192288943Sdim/// If BB is a landingpad block then additional basicblock might be introduced.
193288943Sdim/// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
194288943Sdim/// details on this case.
195288943Sdim///
196296417Sdim/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
197296417Sdim/// no other analyses. In particular, it does not preserve LoopSimplify
198296417Sdim/// (because it's complicated to handle the case where one of the edges being
199296417Sdim/// split is an exit of a loop with other exits).
200288943SdimBasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
201288943Sdim                                   const char *Suffix,
202288943Sdim                                   DominatorTree *DT = nullptr,
203288943Sdim                                   LoopInfo *LI = nullptr,
204288943Sdim                                   bool PreserveLCSSA = false);
205218893Sdim
206309124Sdim/// This method transforms the landing pad, OrigBB, by introducing two new basic
207309124Sdim/// blocks into the function. One of those new basic blocks gets the
208309124Sdim/// predecessors listed in Preds. The other basic block gets the remaining
209309124Sdim/// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both
210309124Sdim/// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and
211309124Sdim/// 'Suffix2', and are returned in the NewBBs vector.
212226633Sdim///
213296417Sdim/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but
214296417Sdim/// no other analyses. In particular, it does not preserve LoopSimplify
215296417Sdim/// (because it's complicated to handle the case where one of the edges being
216296417Sdim/// split is an exit of a loop with other exits).
217288943Sdimvoid SplitLandingPadPredecessors(BasicBlock *OrigBB,
218288943Sdim                                 ArrayRef<BasicBlock *> Preds,
219226633Sdim                                 const char *Suffix, const char *Suffix2,
220288943Sdim                                 SmallVectorImpl<BasicBlock *> &NewBBs,
221288943Sdim                                 DominatorTree *DT = nullptr,
222288943Sdim                                 LoopInfo *LI = nullptr,
223288943Sdim                                 bool PreserveLCSSA = false);
224226633Sdim
225309124Sdim/// This method duplicates the specified return instruction into a predecessor
226309124Sdim/// which ends in an unconditional branch. If the return instruction returns a
227309124Sdim/// value defined by a PHI, propagate the right value into the return. It
228309124Sdim/// returns the new return instruction in the predecessor.
229218893SdimReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
230218893Sdim                                       BasicBlock *Pred);
231218893Sdim
232309124Sdim/// Split the containing block at the specified instruction - everything before
233314564Sdim/// SplitBefore stays in the old basic block, and the rest of the instructions
234314564Sdim/// in the BB are moved to a new block. The two blocks are connected by a
235309124Sdim/// conditional branch (with value of Cmp being the condition).
236243830Sdim/// Before:
237243830Sdim///   Head
238276479Sdim///   SplitBefore
239243830Sdim///   Tail
240243830Sdim/// After:
241243830Sdim///   Head
242276479Sdim///   if (Cond)
243243830Sdim///     ThenBlock
244276479Sdim///   SplitBefore
245243830Sdim///   Tail
246243830Sdim///
247243830Sdim/// If Unreachable is true, then ThenBlock ends with
248243830Sdim/// UnreachableInst, otherwise it branches to Tail.
249243830Sdim/// Returns the NewBasicBlock's terminator.
250276479Sdim///
251309124Sdim/// Updates DT and LI if given.
252276479SdimTerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
253276479Sdim                                          bool Unreachable,
254276479Sdim                                          MDNode *BranchWeights = nullptr,
255309124Sdim                                          DominatorTree *DT = nullptr,
256309124Sdim                                          LoopInfo *LI = nullptr);
257243830Sdim
258276479Sdim/// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
259276479Sdim/// but also creates the ElseBlock.
260276479Sdim/// Before:
261276479Sdim///   Head
262276479Sdim///   SplitBefore
263276479Sdim///   Tail
264276479Sdim/// After:
265276479Sdim///   Head
266276479Sdim///   if (Cond)
267276479Sdim///     ThenBlock
268276479Sdim///   else
269276479Sdim///     ElseBlock
270276479Sdim///   SplitBefore
271276479Sdim///   Tail
272276479Sdimvoid SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
273276479Sdim                                   TerminatorInst **ThenTerm,
274276479Sdim                                   TerminatorInst **ElseTerm,
275276479Sdim                                   MDNode *BranchWeights = nullptr);
276243830Sdim
277309124Sdim/// Check whether BB is the merge point of a if-region.
278261991Sdim/// If so, return the boolean condition that determines which entry into
279261991Sdim/// BB will be taken.  Also, return by references the block that will be
280261991Sdim/// entered from if the condition is true, and the block that will be
281261991Sdim/// entered if the condition is false.
282309124Sdim///
283309124Sdim/// This does no checking to see if the true/false blocks have large or unsavory
284309124Sdim/// instructions in them.
285261991SdimValue *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
286276479Sdim                      BasicBlock *&IfFalse);
287193323Sed
288327952Sdim// Split critical edges where the source of the edge is an indirectbr
289327952Sdim// instruction. This isn't always possible, but we can handle some easy cases.
290327952Sdim// This is useful because MI is unable to split such critical edges,
291327952Sdim// which means it will not be able to sink instructions along those edges.
292327952Sdim// This is especially painful for indirect branches with many successors, where
293327952Sdim// we end up having to prepare all outgoing values in the origin block.
294327952Sdim//
295327952Sdim// Our normal algorithm for splitting critical edges requires us to update
296327952Sdim// the outgoing edges of the edge origin block, but for an indirectbr this
297327952Sdim// is hard, since it would require finding and updating the block addresses
298327952Sdim// the indirect branch uses. But if a block only has a single indirectbr
299327952Sdim// predecessor, with the others being regular branches, we can do it in a
300327952Sdim// different way.
301327952Sdim// Say we have A -> D, B -> D, I -> D where only I -> D is an indirectbr.
302327952Sdim// We can split D into D0 and D1, where D0 contains only the PHIs from D,
303327952Sdim// and D1 is the D block body. We can then duplicate D0 as D0A and D0B, and
304327952Sdim// create the following structure:
305327952Sdim// A -> D0A, B -> D0A, I -> D0B, D0A -> D1, D0B -> D1
306327952Sdim// If BPI and BFI aren't non-null, BPI/BFI will be updated accordingly.
307327952Sdimbool SplitIndirectBrCriticalEdges(Function &F,
308327952Sdim                                  BranchProbabilityInfo *BPI = nullptr,
309327952Sdim                                  BlockFrequencyInfo *BFI = nullptr);
310327952Sdim
311314564Sdim} // end namespace llvm
312314564Sdim
313314564Sdim#endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
314