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. Copies DebugLoc from BI to I, if I doesn't
68 // already have a DebugLoc. The original instruction is deleted and BI is
69 // updated to point to the new instruction.
71 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
72 BasicBlock::iterator &BI, Instruction *I);
74 // ReplaceInstWithInst - Replace the instruction specified by From with the
75 // instruction specified by To. Copies DebugLoc from BI to I, if I doesn't
76 // already have a DebugLoc.
78 void ReplaceInstWithInst(Instruction *From, Instruction *To);
80 /// \brief Option class for critical edge splitting.
82 /// This provides a builder interface for overriding the default options used
83 /// during critical edge splitting.
84 struct CriticalEdgeSplittingOptions {
88 bool MergeIdenticalEdges;
89 bool DontDeleteUselessPHIs;
92 CriticalEdgeSplittingOptions()
93 : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false),
94 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
96 /// \brief Basic case of setting up all the analysis.
97 CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr,
98 LoopInfo *LI = nullptr)
99 : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false),
100 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
102 /// \brief A common pattern is to preserve the dominator tree and loop
103 /// info but not care about AA.
104 CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI)
105 : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false),
106 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
108 CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
109 MergeIdenticalEdges = true;
113 CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
114 DontDeleteUselessPHIs = true;
118 CriticalEdgeSplittingOptions &setPreserveLCSSA() {
119 PreserveLCSSA = true;
124 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
125 /// split the critical edge. This will update the analyses passed in through
126 /// the option struct. This returns the new block if the edge was split, null
129 /// If MergeIdenticalEdges in the options struct is true (not the default),
130 /// *all* edges from TI to the specified successor will be merged into the same
131 /// critical edge block. This is most commonly interesting with switch
132 /// instructions, which may have many edges to any one destination. This
133 /// ensures that all edges to that dest go to one block instead of each going
134 /// to a different block, but isn't the standard definition of a "critical
137 /// It is invalid to call this function on a critical edge that starts at an
138 /// IndirectBrInst. Splitting these edges will almost always create an invalid
139 /// program because the address of the new block won't be the one that is jumped
142 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
143 const CriticalEdgeSplittingOptions &Options =
144 CriticalEdgeSplittingOptions());
147 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
148 const CriticalEdgeSplittingOptions &Options =
149 CriticalEdgeSplittingOptions()) {
150 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
154 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
155 /// false. Otherwise, split all edges between the two blocks and return true.
156 /// This updates all of the same analyses as the other SplitCriticalEdge
157 /// function. If P is specified, it updates the analyses
159 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
160 const CriticalEdgeSplittingOptions &Options =
161 CriticalEdgeSplittingOptions()) {
162 bool MadeChange = false;
163 TerminatorInst *TI = (*PI)->getTerminator();
164 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
165 if (TI->getSuccessor(i) == Succ)
166 MadeChange |= !!SplitCriticalEdge(TI, i, Options);
170 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
171 /// and return true, otherwise return false. This method requires that there be
172 /// an edge between the two blocks. It updates the analyses
173 /// passed in the options struct
175 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
176 const CriticalEdgeSplittingOptions &Options =
177 CriticalEdgeSplittingOptions()) {
178 TerminatorInst *TI = Src->getTerminator();
181 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
182 if (TI->getSuccessor(i) == Dst)
183 return SplitCriticalEdge(TI, i, Options);
188 // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
189 // breaking critical edges as they are found.
190 // Returns the number of broken edges.
191 unsigned SplitAllCriticalEdges(Function &F,
192 const CriticalEdgeSplittingOptions &Options =
193 CriticalEdgeSplittingOptions());
195 /// SplitEdge - Split the edge connecting specified block.
196 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
197 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
199 /// SplitBlock - Split the specified block at the specified instruction - every
200 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
201 /// to a new block. The two blocks are joined by an unconditional branch and
202 /// the loop info is updated.
204 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
205 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
207 /// SplitBlockPredecessors - This method introduces at least one new basic block
208 /// into the function and moves some of the predecessors of BB to be
209 /// predecessors of the new block. The new predecessors are indicated by the
210 /// Preds array. The new block is given a suffix of 'Suffix'. Returns new basic
211 /// block to which predecessors from Preds are now pointing.
213 /// If BB is a landingpad block then additional basicblock might be introduced.
214 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
215 /// details on this case.
217 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
218 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
219 /// In particular, it does not preserve LoopSimplify (because it's
220 /// complicated to handle the case where one of the edges being split
221 /// is an exit of a loop with other exits).
223 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
225 AliasAnalysis *AA = nullptr,
226 DominatorTree *DT = nullptr,
227 LoopInfo *LI = nullptr,
228 bool PreserveLCSSA = false);
230 /// SplitLandingPadPredecessors - This method transforms the landing pad,
231 /// OrigBB, by introducing two new basic blocks into the function. One of those
232 /// new basic blocks gets the predecessors listed in Preds. The other basic
233 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
234 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
235 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
237 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
238 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
239 /// it does not preserve LoopSimplify (because it's complicated to handle the
240 /// case where one of the edges being split is an exit of a loop with other
243 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
244 ArrayRef<BasicBlock *> Preds,
245 const char *Suffix, const char *Suffix2,
246 SmallVectorImpl<BasicBlock *> &NewBBs,
247 AliasAnalysis *AA = nullptr,
248 DominatorTree *DT = nullptr,
249 LoopInfo *LI = nullptr,
250 bool PreserveLCSSA = false);
252 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
253 /// instruction into a predecessor which ends in an unconditional branch. If
254 /// the return instruction returns a value defined by a PHI, propagate the
255 /// right value into the return. It returns the new return instruction in the
257 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
260 /// SplitBlockAndInsertIfThen - Split the containing block at the
261 /// specified instruction - everything before and including SplitBefore stays
262 /// in the old basic block, and everything after SplitBefore is moved to a
263 /// new block. The two blocks are connected by a conditional branch
264 /// (with value of Cmp being the condition).
276 /// If Unreachable is true, then ThenBlock ends with
277 /// UnreachableInst, otherwise it branches to Tail.
278 /// Returns the NewBasicBlock's terminator.
280 /// Updates DT if given.
281 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
283 MDNode *BranchWeights = nullptr,
284 DominatorTree *DT = nullptr);
286 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
287 /// but also creates the ElseBlock.
300 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
301 TerminatorInst **ThenTerm,
302 TerminatorInst **ElseTerm,
303 MDNode *BranchWeights = nullptr);
306 /// GetIfCondition - Check whether BB is the merge point of a if-region.
307 /// If so, return the boolean condition that determines which entry into
308 /// BB will be taken. Also, return by references the block that will be
309 /// entered from if the condition is true, and the block that will be
310 /// entered if the condition is false.
311 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
312 BasicBlock *&IfFalse);
313 } // End llvm namespace