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;
91 CriticalEdgeSplittingOptions()
92 : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false),
93 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
95 /// \brief Basic case of setting up all the analysis.
96 CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr,
97 LoopInfo *LI = nullptr)
98 : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false),
99 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
101 /// \brief A common pattern is to preserve the dominator tree and loop
102 /// info but not care about AA.
103 CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI)
104 : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false),
105 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
107 CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
108 MergeIdenticalEdges = true;
112 CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
113 DontDeleteUselessPHIs = true;
117 CriticalEdgeSplittingOptions &setPreserveLCSSA() {
118 PreserveLCSSA = true;
123 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
124 /// split the critical edge. This will update the analyses passed in through
125 /// the option struct. This returns the new block if the edge was split, null
128 /// If MergeIdenticalEdges in the options struct is true (not the default),
129 /// *all* edges from TI to the specified successor will be merged into the same
130 /// critical edge block. This is most commonly interesting with switch
131 /// instructions, which may have many edges to any one destination. This
132 /// ensures that all edges to that dest go to one block instead of each going
133 /// to a different block, but isn't the standard definition of a "critical
136 /// It is invalid to call this function on a critical edge that starts at an
137 /// IndirectBrInst. Splitting these edges will almost always create an invalid
138 /// program because the address of the new block won't be the one that is jumped
141 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
142 const CriticalEdgeSplittingOptions &Options =
143 CriticalEdgeSplittingOptions());
146 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
147 const CriticalEdgeSplittingOptions &Options =
148 CriticalEdgeSplittingOptions()) {
149 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
153 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
154 /// false. Otherwise, split all edges between the two blocks and return true.
155 /// This updates all of the same analyses as the other SplitCriticalEdge
156 /// function. If P is specified, it updates the analyses
158 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
159 const CriticalEdgeSplittingOptions &Options =
160 CriticalEdgeSplittingOptions()) {
161 bool MadeChange = false;
162 TerminatorInst *TI = (*PI)->getTerminator();
163 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
164 if (TI->getSuccessor(i) == Succ)
165 MadeChange |= !!SplitCriticalEdge(TI, i, Options);
169 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
170 /// and return true, otherwise return false. This method requires that there be
171 /// an edge between the two blocks. It updates the analyses
172 /// passed in the options struct
174 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
175 const CriticalEdgeSplittingOptions &Options =
176 CriticalEdgeSplittingOptions()) {
177 TerminatorInst *TI = Src->getTerminator();
180 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
181 if (TI->getSuccessor(i) == Dst)
182 return SplitCriticalEdge(TI, i, Options);
187 // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
188 // breaking critical edges as they are found.
189 // Returns the number of broken edges.
190 unsigned SplitAllCriticalEdges(Function &F,
191 const CriticalEdgeSplittingOptions &Options =
192 CriticalEdgeSplittingOptions());
194 /// SplitEdge - Split the edge connecting specified block. Pass P must
196 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
198 /// SplitBlock - Split the specified block at the specified instruction - every
199 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
200 /// to a new block. The two blocks are joined by an unconditional branch and
201 /// the loop info is updated.
203 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
204 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
206 /// SplitBlockPredecessors - This method transforms BB by introducing a new
207 /// basic block into the function, and moving some of the predecessors of BB to
208 /// be predecessors of the new block. The new predecessors are indicated by the
209 /// Preds array, which has NumPreds elements in it. The new block is given a
210 /// suffix of 'Suffix'. This function returns the new block.
212 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
213 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
214 /// In particular, it does not preserve LoopSimplify (because it's
215 /// complicated to handle the case where one of the edges being split
216 /// is an exit of a loop with other exits).
218 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
220 AliasAnalysis *AA = nullptr,
221 DominatorTree *DT = nullptr,
222 LoopInfo *LI = nullptr,
223 bool PreserveLCSSA = false);
225 /// SplitLandingPadPredecessors - This method transforms the landing pad,
226 /// OrigBB, by introducing two new basic blocks into the function. One of those
227 /// new basic blocks gets the predecessors listed in Preds. The other basic
228 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
229 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
230 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
232 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
233 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
234 /// it does not preserve LoopSimplify (because it's complicated to handle the
235 /// case where one of the edges being split is an exit of a loop with other
238 void SplitLandingPadPredecessors(BasicBlock *OrigBB,
239 ArrayRef<BasicBlock *> Preds,
240 const char *Suffix, const char *Suffix2,
241 SmallVectorImpl<BasicBlock *> &NewBBs,
242 AliasAnalysis *AA = nullptr,
243 DominatorTree *DT = nullptr,
244 LoopInfo *LI = nullptr,
245 bool PreserveLCSSA = false);
247 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
248 /// instruction into a predecessor which ends in an unconditional branch. If
249 /// the return instruction returns a value defined by a PHI, propagate the
250 /// right value into the return. It returns the new return instruction in the
252 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
255 /// SplitBlockAndInsertIfThen - Split the containing block at the
256 /// specified instruction - everything before and including SplitBefore stays
257 /// in the old basic block, and everything after SplitBefore is moved to a
258 /// new block. The two blocks are connected by a conditional branch
259 /// (with value of Cmp being the condition).
271 /// If Unreachable is true, then ThenBlock ends with
272 /// UnreachableInst, otherwise it branches to Tail.
273 /// Returns the NewBasicBlock's terminator.
275 /// Updates DT if given.
276 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
278 MDNode *BranchWeights = nullptr,
279 DominatorTree *DT = nullptr);
281 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
282 /// but also creates the ElseBlock.
295 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
296 TerminatorInst **ThenTerm,
297 TerminatorInst **ElseTerm,
298 MDNode *BranchWeights = nullptr);
301 /// GetIfCondition - Check whether BB is the merge point of a if-region.
302 /// If so, return the boolean condition that determines which entry into
303 /// BB will be taken. Also, return by references the block that will be
304 /// entered from if the condition is true, and the block that will be
305 /// entered if the condition is false.
306 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
307 BasicBlock *&IfFalse);
308 } // End llvm namespace