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 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
80 /// split the critical edge. This will update DominatorTree and
81 /// DominatorFrontier information if it is available, thus calling this pass
82 /// will not invalidate either of them. This returns the new block if the edge
83 /// was split, null otherwise.
85 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
86 /// specified successor will be merged into the same critical edge block.
87 /// This is most commonly interesting with switch instructions, which may
88 /// have many edges to any one destination. This ensures that all edges to that
89 /// dest go to one block instead of each going to a different block, but isn't
90 /// the standard definition of a "critical edge".
92 /// It is invalid to call this function on a critical edge that starts at an
93 /// IndirectBrInst. Splitting these edges will almost always create an invalid
94 /// program because the address of the new block won't be the one that is jumped
97 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
99 bool MergeIdenticalEdges = false,
100 bool DontDeleteUselessPHIs = false,
101 bool SplitLandingPads = false);
103 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
105 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
108 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
109 /// false. Otherwise, split all edges between the two blocks and return true.
110 /// This updates all of the same analyses as the other SplitCriticalEdge
111 /// function. If P is specified, it updates the analyses
113 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
115 bool MadeChange = false;
116 TerminatorInst *TI = (*PI)->getTerminator();
117 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
118 if (TI->getSuccessor(i) == Succ)
119 MadeChange |= !!SplitCriticalEdge(TI, i, P);
123 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
124 /// and return true, otherwise return false. This method requires that there be
125 /// an edge between the two blocks. If P is specified, it updates the analyses
127 inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
129 bool MergeIdenticalEdges = false,
130 bool DontDeleteUselessPHIs = false) {
131 TerminatorInst *TI = Src->getTerminator();
134 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
135 if (TI->getSuccessor(i) == Dst)
136 return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges,
137 DontDeleteUselessPHIs);
142 // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
143 // breaking critical edges as they are found. Pass P must not be NULL.
144 // Returns the number of broken edges.
145 unsigned SplitAllCriticalEdges(Function &F, Pass *P);
147 /// SplitEdge - Split the edge connecting specified block. Pass P must
149 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
151 /// SplitBlock - Split the specified block at the specified instruction - every
152 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
153 /// to a new block. The two blocks are joined by an unconditional branch and
154 /// the loop info is updated.
156 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
157 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
159 /// SplitBlockPredecessors - This method transforms BB by introducing a new
160 /// basic block into the function, and moving some of the predecessors of BB to
161 /// be predecessors of the new block. The new predecessors are indicated by the
162 /// Preds array, which has NumPreds elements in it. The new block is given a
163 /// suffix of 'Suffix'. This function returns the new block.
165 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
166 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
167 /// In particular, it does not preserve LoopSimplify (because it's
168 /// complicated to handle the case where one of the edges being split
169 /// is an exit of a loop with other exits).
171 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock*> Preds,
172 const char *Suffix, Pass *P = nullptr);
174 /// SplitLandingPadPredecessors - This method transforms the landing pad,
175 /// OrigBB, by introducing two new basic blocks into the function. One of those
176 /// new basic blocks gets the predecessors listed in Preds. The other basic
177 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
178 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
179 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
181 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
182 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
183 /// it does not preserve LoopSimplify (because it's complicated to handle the
184 /// case where one of the edges being split is an exit of a loop with other
187 void SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds,
188 const char *Suffix, const char *Suffix2,
189 Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs);
191 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
192 /// instruction into a predecessor which ends in an unconditional branch. If
193 /// the return instruction returns a value defined by a PHI, propagate the
194 /// right value into the return. It returns the new return instruction in the
196 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
199 /// SplitBlockAndInsertIfThen - Split the containing block at the
200 /// specified instruction - everything before and including SplitBefore stays
201 /// in the old basic block, and everything after SplitBefore is moved to a
202 /// new block. The two blocks are connected by a conditional branch
203 /// (with value of Cmp being the condition).
215 /// If Unreachable is true, then ThenBlock ends with
216 /// UnreachableInst, otherwise it branches to Tail.
217 /// Returns the NewBasicBlock's terminator.
219 /// Updates DT if given.
220 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
222 MDNode *BranchWeights = nullptr,
223 DominatorTree *DT = nullptr);
225 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
226 /// but also creates the ElseBlock.
239 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
240 TerminatorInst **ThenTerm,
241 TerminatorInst **ElseTerm,
242 MDNode *BranchWeights = nullptr);
245 /// GetIfCondition - Check whether BB is the merge point of a if-region.
246 /// If so, return the boolean condition that determines which entry into
247 /// BB will be taken. Also, return by references the block that will be
248 /// entered from if the condition is true, and the block that will be
249 /// entered if the condition is false.
250 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
251 BasicBlock *&IfFalse);
252 } // End llvm namespace