1 //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions perform manipulations on basic blocks, and
11 // instructions contained within basic blocks.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
16 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
18 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/CFG.h"
26 class MemoryDependenceAnalysis;
32 class TargetLibraryInfo;
35 /// DeleteDeadBlock - Delete the specified block, which must have no
37 void DeleteDeadBlock(BasicBlock *BB);
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);
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);
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, DominatorTree *DT = nullptr,
56 LoopInfo *LI = nullptr,
57 AliasAnalysis *AA = nullptr,
58 MemoryDependenceAnalysis *MemDep = nullptr);
60 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
61 // with a value, then remove and delete the original instruction.
63 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
64 BasicBlock::iterator &BI, Value *V);
66 // ReplaceInstWithInst - Replace the instruction specified by BI with the
67 // instruction specified by I. The original instruction is deleted and BI is
68 // updated to point to the new instruction.
70 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
71 BasicBlock::iterator &BI, Instruction *I);
73 // ReplaceInstWithInst - Replace the instruction specified by From with the
74 // instruction specified by To.
76 void ReplaceInstWithInst(Instruction *From, Instruction *To);
78 /// \brief Option class for critical edge splitting.
80 /// This provides a builder interface for overriding the default options used
81 /// during critical edge splitting.
82 struct CriticalEdgeSplittingOptions {
86 bool MergeIdenticalEdges;
87 bool DontDeleteUselessPHIs;
90 CriticalEdgeSplittingOptions()
91 : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false),
92 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
94 /// \brief Basic case of setting up all the analysis.
95 CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr,
96 LoopInfo *LI = nullptr)
97 : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false),
98 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
100 /// \brief A common pattern is to preserve the dominator tree and loop
101 /// info but not care about AA.
102 CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI)
103 : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false),
104 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
106 CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
107 MergeIdenticalEdges = true;
111 CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
112 DontDeleteUselessPHIs = true;
116 CriticalEdgeSplittingOptions &setPreserveLCSSA() {
117 PreserveLCSSA = true;
122 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
123 /// split the critical edge. This will update the analyses passed in through
124 /// the option struct. This returns the new block if the edge was split, null
127 /// If MergeIdenticalEdges in the options struct is true (not the default),
128 /// *all* edges from TI to the specified successor will be merged into the same
129 /// critical edge block. This is most commonly interesting with switch
130 /// instructions, which may have many edges to any one destination. This
131 /// ensures that all edges to that dest go to one block instead of each going
132 /// to a different block, but isn't the standard definition of a "critical
135 /// It is invalid to call this function on a critical edge that starts at an
136 /// IndirectBrInst. Splitting these edges will almost always create an invalid
137 /// program because the address of the new block won't be the one that is jumped
140 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
141 const CriticalEdgeSplittingOptions &Options =
142 CriticalEdgeSplittingOptions());
145 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
146 const CriticalEdgeSplittingOptions &Options =
147 CriticalEdgeSplittingOptions()) {
148 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
152 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
153 /// false. Otherwise, split all edges between the two blocks and return true.
154 /// This updates all of the same analyses as the other SplitCriticalEdge
155 /// function. If P is specified, it updates the analyses
157 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
158 const CriticalEdgeSplittingOptions &Options =
159 CriticalEdgeSplittingOptions()) {
160 bool MadeChange = false;
161 TerminatorInst *TI = (*PI)->getTerminator();
162 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
163 if (TI->getSuccessor(i) == Succ)
164 MadeChange |= !!SplitCriticalEdge(TI, i, Options);
168 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
169 /// and return true, otherwise return false. This method requires that there be
170 /// an edge between the two blocks. It updates the analyses
171 /// passed in the options struct
173 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
174 const CriticalEdgeSplittingOptions &Options =
175 CriticalEdgeSplittingOptions()) {
176 TerminatorInst *TI = Src->getTerminator();
179 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
180 if (TI->getSuccessor(i) == Dst)
181 return SplitCriticalEdge(TI, i, Options);
186 // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
187 // breaking critical edges as they are found.
188 // Returns the number of broken edges.
189 unsigned SplitAllCriticalEdges(Function &F,
190 const CriticalEdgeSplittingOptions &Options =
191 CriticalEdgeSplittingOptions());
193 /// SplitEdge - Split the edge connecting specified block.
194 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
195 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
197 /// SplitBlock - Split the specified block at the specified instruction - every
198 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
199 /// to a new block. The two blocks are joined by an unconditional branch and
200 /// the loop info is updated.
202 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
203 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
205 /// SplitBlockPredecessors - This method introduces at least one new basic block
206 /// into the function and moves some of the predecessors of BB to be
207 /// predecessors of the new block. The new predecessors are indicated by the
208 /// Preds array. The new block is given a suffix of 'Suffix'. Returns new basic
209 /// block to which predecessors from Preds are now pointing.
211 /// If BB is a landingpad block then additional basicblock might be introduced.
212 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
213 /// details on this case.
215 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
216 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
217 /// In particular, it does not preserve LoopSimplify (because it's
218 /// complicated to handle the case where one of the edges being split
219 /// is an exit of a loop with other exits).
221 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
223 AliasAnalysis *AA = nullptr,
224 DominatorTree *DT = nullptr,
225 LoopInfo *LI = nullptr,
226 bool PreserveLCSSA = false);
228 /// SplitLandingPadPredecessors - This method transforms the landing pad,
229 /// OrigBB, by introducing two new basic blocks into the function. One of those
230 /// new basic blocks gets the predecessors listed in Preds. The other basic
231 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
232 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
233 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
235 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
236 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
237 /// it does not preserve LoopSimplify (because it's complicated to handle the
238 /// case where one of the edges being split is an exit of a loop with other
241 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
242 ArrayRef<BasicBlock *> Preds,
243 const char *Suffix, const char *Suffix2,
244 SmallVectorImpl<BasicBlock *> &NewBBs,
245 AliasAnalysis *AA = nullptr,
246 DominatorTree *DT = nullptr,
247 LoopInfo *LI = nullptr,
248 bool PreserveLCSSA = false);
250 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
251 /// instruction into a predecessor which ends in an unconditional branch. If
252 /// the return instruction returns a value defined by a PHI, propagate the
253 /// right value into the return. It returns the new return instruction in the
255 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
258 /// SplitBlockAndInsertIfThen - Split the containing block at the
259 /// specified instruction - everything before and including SplitBefore stays
260 /// in the old basic block, and everything after SplitBefore is moved to a
261 /// new block. The two blocks are connected by a conditional branch
262 /// (with value of Cmp being the condition).
274 /// If Unreachable is true, then ThenBlock ends with
275 /// UnreachableInst, otherwise it branches to Tail.
276 /// Returns the NewBasicBlock's terminator.
278 /// Updates DT if given.
279 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
281 MDNode *BranchWeights = nullptr,
282 DominatorTree *DT = nullptr);
284 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
285 /// but also creates the ElseBlock.
298 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
299 TerminatorInst **ThenTerm,
300 TerminatorInst **ElseTerm,
301 MDNode *BranchWeights = nullptr);
304 /// GetIfCondition - Check whether BB is the merge point of a if-region.
305 /// If so, return the boolean condition that determines which entry into
306 /// BB will be taken. Also, return by references the block that will be
307 /// entered from if the condition is true, and the block that will be
308 /// entered if the condition is false.
309 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
310 BasicBlock *&IfFalse);
311 } // End llvm namespace