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;
33 class TargetLibraryInfo;
36 /// DeleteDeadBlock - Delete the specified block, which must have no
38 void DeleteDeadBlock(BasicBlock *BB);
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);
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);
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);
61 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
62 // with a value, then remove and delete the original instruction.
64 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
65 BasicBlock::iterator &BI, Value *V);
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.
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.
77 void ReplaceInstWithInst(Instruction *From, Instruction *To);
79 /// \brief Option class for critical edge splitting.
81 /// This provides a builder interface for overriding the default options used
82 /// during critical edge splitting.
83 struct CriticalEdgeSplittingOptions {
87 bool MergeIdenticalEdges;
88 bool DontDeleteUselessPHIs;
89 bool SplitLandingPads;
92 CriticalEdgeSplittingOptions()
93 : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false),
94 DontDeleteUselessPHIs(false), SplitLandingPads(false),
95 PreserveLCSSA(false) {}
97 /// \brief Basic case of setting up all the analysis.
98 CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr,
99 LoopInfo *LI = nullptr)
100 : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false),
101 DontDeleteUselessPHIs(false), SplitLandingPads(false),
102 PreserveLCSSA(false) {}
104 /// \brief A common pattern is to preserve the dominator tree and loop
105 /// info but not care about AA.
106 CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI)
107 : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false),
108 DontDeleteUselessPHIs(false), SplitLandingPads(false),
109 PreserveLCSSA(false) {}
111 CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
112 MergeIdenticalEdges = true;
116 CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
117 DontDeleteUselessPHIs = true;
121 CriticalEdgeSplittingOptions &setSplitLandingPads() {
122 SplitLandingPads = true;
126 CriticalEdgeSplittingOptions &setPreserveLCSSA() {
127 PreserveLCSSA = true;
132 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
133 /// split the critical edge. This will update the analyses passed in through
134 /// the option struct. This returns the new block if the edge was split, null
137 /// If MergeIdenticalEdges in the options struct is true (not the default),
138 /// *all* edges from TI to the specified successor will be merged into the same
139 /// critical edge block. This is most commonly interesting with switch
140 /// instructions, which may have many edges to any one destination. This
141 /// ensures that all edges to that dest go to one block instead of each going
142 /// to a different block, but isn't the standard definition of a "critical
145 /// It is invalid to call this function on a critical edge that starts at an
146 /// IndirectBrInst. Splitting these edges will almost always create an invalid
147 /// program because the address of the new block won't be the one that is jumped
150 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
151 const CriticalEdgeSplittingOptions &Options =
152 CriticalEdgeSplittingOptions());
155 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
156 const CriticalEdgeSplittingOptions &Options =
157 CriticalEdgeSplittingOptions()) {
158 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
162 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
163 /// false. Otherwise, split all edges between the two blocks and return true.
164 /// This updates all of the same analyses as the other SplitCriticalEdge
165 /// function. If P is specified, it updates the analyses
167 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
168 const CriticalEdgeSplittingOptions &Options =
169 CriticalEdgeSplittingOptions()) {
170 bool MadeChange = false;
171 TerminatorInst *TI = (*PI)->getTerminator();
172 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
173 if (TI->getSuccessor(i) == Succ)
174 MadeChange |= !!SplitCriticalEdge(TI, i, Options);
178 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
179 /// and return true, otherwise return false. This method requires that there be
180 /// an edge between the two blocks. It updates the analyses
181 /// passed in the options struct
183 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
184 const CriticalEdgeSplittingOptions &Options =
185 CriticalEdgeSplittingOptions()) {
186 TerminatorInst *TI = Src->getTerminator();
189 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
190 if (TI->getSuccessor(i) == Dst)
191 return SplitCriticalEdge(TI, i, Options);
196 // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
197 // breaking critical edges as they are found.
198 // Returns the number of broken edges.
199 unsigned SplitAllCriticalEdges(Function &F,
200 const CriticalEdgeSplittingOptions &Options =
201 CriticalEdgeSplittingOptions());
203 /// SplitEdge - Split the edge connecting specified block. Pass P must
205 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
207 /// SplitBlock - Split the specified block at the specified instruction - every
208 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
209 /// to a new block. The two blocks are joined by an unconditional branch and
210 /// the loop info is updated.
212 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
213 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
215 /// SplitBlockPredecessors - This method transforms BB by introducing a new
216 /// basic block into the function, and moving some of the predecessors of BB to
217 /// be predecessors of the new block. The new predecessors are indicated by the
218 /// Preds array, which has NumPreds elements in it. The new block is given a
219 /// suffix of 'Suffix'. This function returns the new block.
221 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
222 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
223 /// In particular, it does not preserve LoopSimplify (because it's
224 /// complicated to handle the case where one of the edges being split
225 /// is an exit of a loop with other exits).
227 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
229 AliasAnalysis *AA = nullptr,
230 DominatorTree *DT = nullptr,
231 LoopInfo *LI = nullptr,
232 bool PreserveLCSSA = false);
234 /// SplitLandingPadPredecessors - This method transforms the landing pad,
235 /// OrigBB, by introducing two new basic blocks into the function. One of those
236 /// new basic blocks gets the predecessors listed in Preds. The other basic
237 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
238 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
239 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
241 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
242 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
243 /// it does not preserve LoopSimplify (because it's complicated to handle the
244 /// case where one of the edges being split is an exit of a loop with other
247 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
248 ArrayRef<BasicBlock *> Preds,
249 const char *Suffix, const char *Suffix2,
250 SmallVectorImpl<BasicBlock *> &NewBBs,
251 AliasAnalysis *AA = nullptr,
252 DominatorTree *DT = nullptr,
253 LoopInfo *LI = nullptr,
254 bool PreserveLCSSA = false);
256 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
257 /// instruction into a predecessor which ends in an unconditional branch. If
258 /// the return instruction returns a value defined by a PHI, propagate the
259 /// right value into the return. It returns the new return instruction in the
261 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
264 /// SplitBlockAndInsertIfThen - Split the containing block at the
265 /// specified instruction - everything before and including SplitBefore stays
266 /// in the old basic block, and everything after SplitBefore is moved to a
267 /// new block. The two blocks are connected by a conditional branch
268 /// (with value of Cmp being the condition).
280 /// If Unreachable is true, then ThenBlock ends with
281 /// UnreachableInst, otherwise it branches to Tail.
282 /// Returns the NewBasicBlock's terminator.
284 /// Updates DT if given.
285 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
287 MDNode *BranchWeights = nullptr,
288 DominatorTree *DT = nullptr);
290 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
291 /// but also creates the ElseBlock.
304 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
305 TerminatorInst **ThenTerm,
306 TerminatorInst **ElseTerm,
307 MDNode *BranchWeights = nullptr);
310 /// GetIfCondition - Check whether BB is the merge point of a if-region.
311 /// If so, return the boolean condition that determines which entry into
312 /// BB will be taken. Also, return by references the block that will be
313 /// entered from if the condition is true, and the block that will be
314 /// entered if the condition is false.
315 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
316 BasicBlock *&IfFalse);
317 } // End llvm namespace