1//===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This family of functions perform manipulations on basic blocks, and
11// instructions contained within basic blocks.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
16#define LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
17
18// FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
19
20#include "llvm/BasicBlock.h"
21#include "llvm/Support/CFG.h"
22#include "llvm/Support/DebugLoc.h"
23
24namespace llvm {
25
26class AliasAnalysis;
27class Instruction;
28class Pass;
29class ReturnInst;
30class TargetLibraryInfo;
31
32/// DeleteDeadBlock - Delete the specified block, which must have no
33/// predecessors.
34void DeleteDeadBlock(BasicBlock *BB);
35
36
37/// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are
38/// any single-entry PHI nodes in it, fold them away.  This handles the case
39/// when all entries to the PHI nodes in a block are guaranteed equal, such as
40/// when the block has exactly one predecessor.
41void FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0);
42
43/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
44/// is dead. Also recursively delete any operands that become dead as
45/// a result. This includes tracing the def-use list from the PHI to see if
46/// it is ultimately unused or if it reaches an unused cycle. Return true
47/// if any PHIs were deleted.
48bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = 0);
49
50/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
51/// if possible.  The return value indicates success or failure.
52bool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0);
53
54// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
55// with a value, then remove and delete the original instruction.
56//
57void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
58                          BasicBlock::iterator &BI, Value *V);
59
60// ReplaceInstWithInst - Replace the instruction specified by BI with the
61// instruction specified by I.  The original instruction is deleted and BI is
62// updated to point to the new instruction.
63//
64void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
65                         BasicBlock::iterator &BI, Instruction *I);
66
67// ReplaceInstWithInst - Replace the instruction specified by From with the
68// instruction specified by To.
69//
70void ReplaceInstWithInst(Instruction *From, Instruction *To);
71
72/// FindFunctionBackedges - Analyze the specified function to find all of the
73/// loop backedges in the function and return them.  This is a relatively cheap
74/// (compared to computing dominators and loop info) analysis.
75///
76/// The output is added to Result, as pairs of <from,to> edge info.
77void FindFunctionBackedges(const Function &F,
78      SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result);
79
80
81/// GetSuccessorNumber - Search for the specified successor of basic block BB
82/// and return its position in the terminator instruction's list of
83/// successors.  It is an error to call this with a block that is not a
84/// successor.
85unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ);
86
87/// isCriticalEdge - Return true if the specified edge is a critical edge.
88/// Critical edges are edges from a block with multiple successors to a block
89/// with multiple predecessors.
90///
91bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
92                    bool AllowIdenticalEdges = false);
93
94/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
95/// split the critical edge.  This will update DominatorTree and
96/// DominatorFrontier information if it is available, thus calling this pass
97/// will not invalidate either of them. This returns the new block if the edge
98/// was split, null otherwise.
99///
100/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
101/// specified successor will be merged into the same critical edge block.
102/// This is most commonly interesting with switch instructions, which may
103/// have many edges to any one destination.  This ensures that all edges to that
104/// dest go to one block instead of each going to a different block, but isn't
105/// the standard definition of a "critical edge".
106///
107/// It is invalid to call this function on a critical edge that starts at an
108/// IndirectBrInst.  Splitting these edges will almost always create an invalid
109/// program because the address of the new block won't be the one that is jumped
110/// to.
111///
112BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
113                              Pass *P = 0, bool MergeIdenticalEdges = false,
114                              bool DontDeleteUselessPHIs = false,
115                              bool SplitLandingPads = false);
116
117inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
118                                     Pass *P = 0) {
119  return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
120}
121
122/// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
123/// false.  Otherwise, split all edges between the two blocks and return true.
124/// This updates all of the same analyses as the other SplitCriticalEdge
125/// function.  If P is specified, it updates the analyses
126/// described above.
127inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
128  bool MadeChange = false;
129  TerminatorInst *TI = (*PI)->getTerminator();
130  for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
131    if (TI->getSuccessor(i) == Succ)
132      MadeChange |= !!SplitCriticalEdge(TI, i, P);
133  return MadeChange;
134}
135
136/// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
137/// and return true, otherwise return false.  This method requires that there be
138/// an edge between the two blocks.  If P is specified, it updates the analyses
139/// described above.
140inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
141                                     Pass *P = 0,
142                                     bool MergeIdenticalEdges = false,
143                                     bool DontDeleteUselessPHIs = false) {
144  TerminatorInst *TI = Src->getTerminator();
145  unsigned i = 0;
146  while (1) {
147    assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
148    if (TI->getSuccessor(i) == Dst)
149      return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges,
150                               DontDeleteUselessPHIs);
151    ++i;
152  }
153}
154
155/// SplitEdge -  Split the edge connecting specified block. Pass P must
156/// not be NULL.
157BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
158
159/// SplitBlock - Split the specified block at the specified instruction - every
160/// thing before SplitPt stays in Old and everything starting with SplitPt moves
161/// to a new block.  The two blocks are joined by an unconditional branch and
162/// the loop info is updated.
163///
164BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
165
166/// SplitBlockPredecessors - This method transforms BB by introducing a new
167/// basic block into the function, and moving some of the predecessors of BB to
168/// be predecessors of the new block.  The new predecessors are indicated by the
169/// Preds array, which has NumPreds elements in it.  The new block is given a
170/// suffix of 'Suffix'.  This function returns the new block.
171///
172/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
173/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
174/// In particular, it does not preserve LoopSimplify (because it's
175/// complicated to handle the case where one of the edges being split
176/// is an exit of a loop with other exits).
177///
178BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock*> Preds,
179                                   const char *Suffix, Pass *P = 0);
180
181/// SplitLandingPadPredecessors - This method transforms the landing pad,
182/// OrigBB, by introducing two new basic blocks into the function. One of those
183/// new basic blocks gets the predecessors listed in Preds. The other basic
184/// block gets the remaining predecessors of OrigBB. The landingpad instruction
185/// OrigBB is clone into both of the new basic blocks. The new blocks are given
186/// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
187///
188/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
189/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
190/// it does not preserve LoopSimplify (because it's complicated to handle the
191/// case where one of the edges being split is an exit of a loop with other
192/// exits).
193///
194void SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds,
195                                 const char *Suffix, const char *Suffix2,
196                                 Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs);
197
198/// FoldReturnIntoUncondBranch - This method duplicates the specified return
199/// instruction into a predecessor which ends in an unconditional branch. If
200/// the return instruction returns a value defined by a PHI, propagate the
201/// right value into the return. It returns the new return instruction in the
202/// predecessor.
203ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
204                                       BasicBlock *Pred);
205
206} // End llvm namespace
207
208#endif
209