[PM] Lift the analyses into the interface for
[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,
157                        DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
158
159 /// SplitBlockPredecessors - This method transforms BB by introducing a new
160 /// basic block into the function, and moving some of the predecessors of BB to
161 /// be predecessors of the new block.  The new predecessors are indicated by the
162 /// Preds array, which has NumPreds elements in it.  The new block is given a
163 /// suffix of 'Suffix'.  This function returns the new block.
164 ///
165 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
166 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
167 /// In particular, it does not preserve LoopSimplify (because it's
168 /// complicated to handle the case where one of the edges being split
169 /// is an exit of a loop with other exits).
170 ///
171 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
172                                    const char *Suffix,
173                                    AliasAnalysis *AA = nullptr,
174                                    DominatorTree *DT = nullptr,
175                                    LoopInfo *LI = nullptr,
176                                    bool PreserveLCSSA = false);
177
178 /// SplitLandingPadPredecessors - This method transforms the landing pad,
179 /// OrigBB, by introducing two new basic blocks into the function. One of those
180 /// new basic blocks gets the predecessors listed in Preds. The other basic
181 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
182 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
183 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
184 ///
185 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
186 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
187 /// it does not preserve LoopSimplify (because it's complicated to handle the
188 /// case where one of the edges being split is an exit of a loop with other
189 /// exits).
190 ///
191 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
192                                  ArrayRef<BasicBlock *> Preds,
193                                  const char *Suffix, const char *Suffix2,
194                                  SmallVectorImpl<BasicBlock *> &NewBBs,
195                                  AliasAnalysis *AA = nullptr,
196                                  DominatorTree *DT = nullptr,
197                                  LoopInfo *LI = nullptr,
198                                  bool PreserveLCSSA = false);
199
200 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
201 /// instruction into a predecessor which ends in an unconditional branch. If
202 /// the return instruction returns a value defined by a PHI, propagate the
203 /// right value into the return. It returns the new return instruction in the
204 /// predecessor.
205 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
206                                        BasicBlock *Pred);
207
208 /// SplitBlockAndInsertIfThen - Split the containing block at the
209 /// specified instruction - everything before and including SplitBefore stays
210 /// in the old basic block, and everything after SplitBefore is moved to a
211 /// new block. The two blocks are connected by a conditional branch
212 /// (with value of Cmp being the condition).
213 /// Before:
214 ///   Head
215 ///   SplitBefore
216 ///   Tail
217 /// After:
218 ///   Head
219 ///   if (Cond)
220 ///     ThenBlock
221 ///   SplitBefore
222 ///   Tail
223 ///
224 /// If Unreachable is true, then ThenBlock ends with
225 /// UnreachableInst, otherwise it branches to Tail.
226 /// Returns the NewBasicBlock's terminator.
227 ///
228 /// Updates DT if given.
229 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
230                                           bool Unreachable,
231                                           MDNode *BranchWeights = nullptr,
232                                           DominatorTree *DT = nullptr);
233
234 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
235 /// but also creates the ElseBlock.
236 /// Before:
237 ///   Head
238 ///   SplitBefore
239 ///   Tail
240 /// After:
241 ///   Head
242 ///   if (Cond)
243 ///     ThenBlock
244 ///   else
245 ///     ElseBlock
246 ///   SplitBefore
247 ///   Tail
248 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
249                                    TerminatorInst **ThenTerm,
250                                    TerminatorInst **ElseTerm,
251                                    MDNode *BranchWeights = nullptr);
252
253 ///
254 /// GetIfCondition - Check whether BB is the merge point of a if-region.
255 /// If so, return the boolean condition that determines which entry into
256 /// BB will be taken.  Also, return by references the block that will be
257 /// entered from if the condition is true, and the block that will be
258 /// entered if the condition is false.
259 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
260                       BasicBlock *&IfFalse);
261 } // End llvm namespace
262
263 #endif