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