Reformat blank lines.
[oota-llvm.git] / include / llvm / Transforms / Utils / BasicBlockUtils.h
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_BASICBLOCKUTILS_H
16 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
17
18 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
19
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/CFG.h"
22
23 namespace llvm {
24
25 class AliasAnalysis;
26 class Instruction;
27 class MDNode;
28 class Pass;
29 class ReturnInst;
30 class TargetLibraryInfo;
31 class TerminatorInst;
32
33 /// DeleteDeadBlock - Delete the specified block, which must have no
34 /// predecessors.
35 void DeleteDeadBlock(BasicBlock *BB);
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.
41 void FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = nullptr);
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.
48 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr);
49
50 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
51 /// if possible.  The return value indicates success or failure.
52 bool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = nullptr);
53
54 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
55 // with a value, then remove and delete the original instruction.
56 //
57 void 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 //
64 void 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 //
70 void ReplaceInstWithInst(Instruction *From, Instruction *To);
71
72 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
73 /// split the critical edge.  This will update DominatorTree and
74 /// DominatorFrontier information if it is available, thus calling this pass
75 /// will not invalidate either of them. This returns the new block if the edge
76 /// was split, null otherwise.
77 ///
78 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
79 /// specified successor will be merged into the same critical edge block.
80 /// This is most commonly interesting with switch instructions, which may
81 /// have many edges to any one destination.  This ensures that all edges to that
82 /// dest go to one block instead of each going to a different block, but isn't
83 /// the standard definition of a "critical edge".
84 ///
85 /// It is invalid to call this function on a critical edge that starts at an
86 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
87 /// program because the address of the new block won't be the one that is jumped
88 /// to.
89 ///
90 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
91                               Pass *P = nullptr,
92                               bool MergeIdenticalEdges = false,
93                               bool DontDeleteUselessPHIs = false,
94                               bool SplitLandingPads = false);
95
96 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
97                                      Pass *P = nullptr) {
98   return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
99 }
100
101 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
102 /// false.  Otherwise, split all edges between the two blocks and return true.
103 /// This updates all of the same analyses as the other SplitCriticalEdge
104 /// function.  If P is specified, it updates the analyses
105 /// described above.
106 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
107                               Pass *P = nullptr) {
108   bool MadeChange = false;
109   TerminatorInst *TI = (*PI)->getTerminator();
110   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
111     if (TI->getSuccessor(i) == Succ)
112       MadeChange |= !!SplitCriticalEdge(TI, i, P);
113   return MadeChange;
114 }
115
116 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
117 /// and return true, otherwise return false.  This method requires that there be
118 /// an edge between the two blocks.  If P is specified, it updates the analyses
119 /// described above.
120 inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
121                                      Pass *P = nullptr,
122                                      bool MergeIdenticalEdges = false,
123                                      bool DontDeleteUselessPHIs = false) {
124   TerminatorInst *TI = Src->getTerminator();
125   unsigned i = 0;
126   while (1) {
127     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
128     if (TI->getSuccessor(i) == Dst)
129       return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges,
130                                DontDeleteUselessPHIs);
131     ++i;
132   }
133 }
134
135 /// SplitEdge -  Split the edge connecting specified block. Pass P must
136 /// not be NULL.
137 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
138
139 /// SplitBlock - Split the specified block at the specified instruction - every
140 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
141 /// to a new block.  The two blocks are joined by an unconditional branch and
142 /// the loop info is updated.
143 ///
144 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
145
146 /// SplitBlockPredecessors - This method transforms BB by introducing a new
147 /// basic block into the function, and moving some of the predecessors of BB to
148 /// be predecessors of the new block.  The new predecessors are indicated by the
149 /// Preds array, which has NumPreds elements in it.  The new block is given a
150 /// suffix of 'Suffix'.  This function returns the new block.
151 ///
152 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
153 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
154 /// In particular, it does not preserve LoopSimplify (because it's
155 /// complicated to handle the case where one of the edges being split
156 /// is an exit of a loop with other exits).
157 ///
158 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock*> Preds,
159                                    const char *Suffix, Pass *P = nullptr);
160
161 /// SplitLandingPadPredecessors - This method transforms the landing pad,
162 /// OrigBB, by introducing two new basic blocks into the function. One of those
163 /// new basic blocks gets the predecessors listed in Preds. The other basic
164 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
165 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
166 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
167 ///
168 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
169 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
170 /// it does not preserve LoopSimplify (because it's complicated to handle the
171 /// case where one of the edges being split is an exit of a loop with other
172 /// exits).
173 ///
174 void SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds,
175                                  const char *Suffix, const char *Suffix2,
176                                  Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs);
177
178 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
179 /// instruction into a predecessor which ends in an unconditional branch. If
180 /// the return instruction returns a value defined by a PHI, propagate the
181 /// right value into the return. It returns the new return instruction in the
182 /// predecessor.
183 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
184                                        BasicBlock *Pred);
185
186 /// SplitBlockAndInsertIfThen - Split the containing block at the
187 /// specified instruction - everything before and including SplitBefore stays
188 /// in the old basic block, and everything after SplitBefore is moved to a
189 /// new block. The two blocks are connected by a conditional branch
190 /// (with value of Cmp being the condition).
191 /// Before:
192 ///   Head
193 ///   SplitBefore
194 ///   Tail
195 /// After:
196 ///   Head
197 ///   if (Cond)
198 ///     ThenBlock
199 ///   SplitBefore
200 ///   Tail
201 ///
202 /// If Unreachable is true, then ThenBlock ends with
203 /// UnreachableInst, otherwise it branches to Tail.
204 /// Returns the NewBasicBlock's terminator.
205 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
206                                           bool Unreachable,
207                                           MDNode *BranchWeights = nullptr);
208
209 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
210 /// but also creates the ElseBlock.
211 /// Before:
212 ///   Head
213 ///   SplitBefore
214 ///   Tail
215 /// After:
216 ///   Head
217 ///   if (Cond)
218 ///     ThenBlock
219 ///   else
220 ///     ElseBlock
221 ///   SplitBefore
222 ///   Tail
223 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
224                                    TerminatorInst **ThenTerm,
225                                    TerminatorInst **ElseTerm,
226                                    MDNode *BranchWeights = nullptr);
227
228 ///
229 /// GetIfCondition - Check whether BB is the merge point of a if-region.
230 /// If so, return the boolean condition that determines which entry into
231 /// BB will be taken.  Also, return by references the block that will be
232 /// entered from if the condition is true, and the block that will be
233 /// entered if the condition is false.
234 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
235                       BasicBlock *&IfFalse);
236 } // End llvm namespace
237
238 #endif