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