[PM] Cleanup a dead option to critical edge splitting that I noticed
[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 /// \brief Option class for critical edge splitting.
80 ///
81 /// This provides a builder interface for overriding the default options used
82 /// during critical edge splitting.
83 struct CriticalEdgeSplittingOptions {
84   AliasAnalysis *AA;
85   DominatorTree *DT;
86   LoopInfo *LI;
87   bool MergeIdenticalEdges;
88   bool DontDeleteUselessPHIs;
89   bool PreserveLCSSA;
90
91   CriticalEdgeSplittingOptions()
92       : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false),
93         DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
94
95   /// \brief Basic case of setting up all the analysis.
96   CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr,
97                                LoopInfo *LI = nullptr)
98       : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false),
99         DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
100
101   /// \brief A common pattern is to preserve the dominator tree and loop
102   /// info but not care about AA.
103   CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI)
104       : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false),
105         DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
106
107   CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
108     MergeIdenticalEdges = true;
109     return *this;
110   }
111
112   CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
113     DontDeleteUselessPHIs = true;
114     return *this;
115   }
116
117   CriticalEdgeSplittingOptions &setPreserveLCSSA() {
118     PreserveLCSSA = true;
119     return *this;
120   }
121 };
122
123 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
124 /// split the critical edge.  This will update the analyses passed in through
125 /// the option struct. This returns the new block if the edge was split, null
126 /// otherwise.
127 ///
128 /// If MergeIdenticalEdges in the options struct is true (not the default),
129 /// *all* edges from TI to the specified successor will be merged into the same
130 /// critical edge block. This is most commonly interesting with switch
131 /// instructions, which may have many edges to any one destination.  This
132 /// ensures that all edges to that dest go to one block instead of each going
133 /// to a different block, but isn't the standard definition of a "critical
134 /// edge".
135 ///
136 /// It is invalid to call this function on a critical edge that starts at an
137 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
138 /// program because the address of the new block won't be the one that is jumped
139 /// to.
140 ///
141 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
142                               const CriticalEdgeSplittingOptions &Options =
143                                   CriticalEdgeSplittingOptions());
144
145 inline BasicBlock *
146 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
147                   const CriticalEdgeSplittingOptions &Options =
148                       CriticalEdgeSplittingOptions()) {
149   return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
150                            Options);
151 }
152
153 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
154 /// false.  Otherwise, split all edges between the two blocks and return true.
155 /// This updates all of the same analyses as the other SplitCriticalEdge
156 /// function.  If P is specified, it updates the analyses
157 /// described above.
158 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
159                               const CriticalEdgeSplittingOptions &Options =
160                                   CriticalEdgeSplittingOptions()) {
161   bool MadeChange = false;
162   TerminatorInst *TI = (*PI)->getTerminator();
163   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
164     if (TI->getSuccessor(i) == Succ)
165       MadeChange |= !!SplitCriticalEdge(TI, i, Options);
166   return MadeChange;
167 }
168
169 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
170 /// and return true, otherwise return false.  This method requires that there be
171 /// an edge between the two blocks.  It updates the analyses
172 /// passed in the options struct
173 inline BasicBlock *
174 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
175                   const CriticalEdgeSplittingOptions &Options =
176                       CriticalEdgeSplittingOptions()) {
177   TerminatorInst *TI = Src->getTerminator();
178   unsigned i = 0;
179   while (1) {
180     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
181     if (TI->getSuccessor(i) == Dst)
182       return SplitCriticalEdge(TI, i, Options);
183     ++i;
184   }
185 }
186
187 // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
188 // breaking critical edges as they are found.
189 // Returns the number of broken edges.
190 unsigned SplitAllCriticalEdges(Function &F,
191                                const CriticalEdgeSplittingOptions &Options =
192                                    CriticalEdgeSplittingOptions());
193
194 /// SplitEdge -  Split the edge connecting specified block. Pass P must
195 /// not be NULL.
196 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
197
198 /// SplitBlock - Split the specified block at the specified instruction - every
199 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
200 /// to a new block.  The two blocks are joined by an unconditional branch and
201 /// the loop info is updated.
202 ///
203 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
204                        DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
205
206 /// SplitBlockPredecessors - This method transforms BB by introducing a new
207 /// basic block into the function, and moving some of the predecessors of BB to
208 /// be predecessors of the new block.  The new predecessors are indicated by the
209 /// Preds array, which has NumPreds elements in it.  The new block is given a
210 /// suffix of 'Suffix'.  This function returns the new block.
211 ///
212 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
213 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
214 /// In particular, it does not preserve LoopSimplify (because it's
215 /// complicated to handle the case where one of the edges being split
216 /// is an exit of a loop with other exits).
217 ///
218 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
219                                    const char *Suffix,
220                                    AliasAnalysis *AA = nullptr,
221                                    DominatorTree *DT = nullptr,
222                                    LoopInfo *LI = nullptr,
223                                    bool PreserveLCSSA = false);
224
225 /// SplitLandingPadPredecessors - This method transforms the landing pad,
226 /// OrigBB, by introducing two new basic blocks into the function. One of those
227 /// new basic blocks gets the predecessors listed in Preds. The other basic
228 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
229 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
230 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
231 ///
232 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
233 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
234 /// it does not preserve LoopSimplify (because it's complicated to handle the
235 /// case where one of the edges being split is an exit of a loop with other
236 /// exits).
237 ///
238 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
239                                  ArrayRef<BasicBlock *> Preds,
240                                  const char *Suffix, const char *Suffix2,
241                                  SmallVectorImpl<BasicBlock *> &NewBBs,
242                                  AliasAnalysis *AA = nullptr,
243                                  DominatorTree *DT = nullptr,
244                                  LoopInfo *LI = nullptr,
245                                  bool PreserveLCSSA = false);
246
247 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
248 /// instruction into a predecessor which ends in an unconditional branch. If
249 /// the return instruction returns a value defined by a PHI, propagate the
250 /// right value into the return. It returns the new return instruction in the
251 /// predecessor.
252 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
253                                        BasicBlock *Pred);
254
255 /// SplitBlockAndInsertIfThen - Split the containing block at the
256 /// specified instruction - everything before and including SplitBefore stays
257 /// in the old basic block, and everything after SplitBefore is moved to a
258 /// new block. The two blocks are connected by a conditional branch
259 /// (with value of Cmp being the condition).
260 /// Before:
261 ///   Head
262 ///   SplitBefore
263 ///   Tail
264 /// After:
265 ///   Head
266 ///   if (Cond)
267 ///     ThenBlock
268 ///   SplitBefore
269 ///   Tail
270 ///
271 /// If Unreachable is true, then ThenBlock ends with
272 /// UnreachableInst, otherwise it branches to Tail.
273 /// Returns the NewBasicBlock's terminator.
274 ///
275 /// Updates DT if given.
276 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
277                                           bool Unreachable,
278                                           MDNode *BranchWeights = nullptr,
279                                           DominatorTree *DT = nullptr);
280
281 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
282 /// but also creates the ElseBlock.
283 /// Before:
284 ///   Head
285 ///   SplitBefore
286 ///   Tail
287 /// After:
288 ///   Head
289 ///   if (Cond)
290 ///     ThenBlock
291 ///   else
292 ///     ElseBlock
293 ///   SplitBefore
294 ///   Tail
295 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
296                                    TerminatorInst **ThenTerm,
297                                    TerminatorInst **ElseTerm,
298                                    MDNode *BranchWeights = nullptr);
299
300 ///
301 /// GetIfCondition - Check whether BB is the merge point of a if-region.
302 /// If so, return the boolean condition that determines which entry into
303 /// BB will be taken.  Also, return by references the block that will be
304 /// entered from if the condition is true, and the block that will be
305 /// entered if the condition is false.
306 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
307                       BasicBlock *&IfFalse);
308 } // End llvm namespace
309
310 #endif