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