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/Support/CFG.h"
30 class TargetLibraryInfo;
33 /// DeleteDeadBlock - Delete the specified block, which must have no
35 void DeleteDeadBlock(BasicBlock *BB);
38 /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
39 /// any single-entry PHI nodes in it, fold them away. This handles the case
40 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
41 /// when the block has exactly one predecessor.
42 void FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0);
44 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
45 /// is dead. Also recursively delete any operands that become dead as
46 /// a result. This includes tracing the def-use list from the PHI to see if
47 /// it is ultimately unused or if it reaches an unused cycle. Return true
48 /// if any PHIs were deleted.
49 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = 0);
51 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
52 /// if possible. The return value indicates success or failure.
53 bool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0);
55 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
56 // with a value, then remove and delete the original instruction.
58 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
59 BasicBlock::iterator &BI, Value *V);
61 // ReplaceInstWithInst - Replace the instruction specified by BI with the
62 // instruction specified by I. The original instruction is deleted and BI is
63 // updated to point to the new instruction.
65 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
66 BasicBlock::iterator &BI, Instruction *I);
68 // ReplaceInstWithInst - Replace the instruction specified by From with the
69 // instruction specified by To.
71 void ReplaceInstWithInst(Instruction *From, Instruction *To);
73 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
74 /// split the critical edge. This will update DominatorTree and
75 /// DominatorFrontier information if it is available, thus calling this pass
76 /// will not invalidate either of them. This returns the new block if the edge
77 /// was split, null otherwise.
79 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
80 /// specified successor will be merged into the same critical edge block.
81 /// This is most commonly interesting with switch instructions, which may
82 /// have many edges to any one destination. This ensures that all edges to that
83 /// dest go to one block instead of each going to a different block, but isn't
84 /// the standard definition of a "critical edge".
86 /// It is invalid to call this function on a critical edge that starts at an
87 /// IndirectBrInst. Splitting these edges will almost always create an invalid
88 /// program because the address of the new block won't be the one that is jumped
91 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
92 Pass *P = 0, bool MergeIdenticalEdges = false,
93 bool DontDeleteUselessPHIs = false,
94 bool SplitLandingPads = false);
96 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
98 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
101 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
102 /// false. Otherwise, split all edges between the two blocks and return true.
103 /// This updates all of the same analyses as the other SplitCriticalEdge
104 /// function. If P is specified, it updates the analyses
106 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
107 bool MadeChange = false;
108 TerminatorInst *TI = (*PI)->getTerminator();
109 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
110 if (TI->getSuccessor(i) == Succ)
111 MadeChange |= !!SplitCriticalEdge(TI, i, P);
115 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
116 /// and return true, otherwise return false. This method requires that there be
117 /// an edge between the two blocks. If P is specified, it updates the analyses
119 inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
121 bool MergeIdenticalEdges = false,
122 bool DontDeleteUselessPHIs = false) {
123 TerminatorInst *TI = Src->getTerminator();
126 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
127 if (TI->getSuccessor(i) == Dst)
128 return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges,
129 DontDeleteUselessPHIs);
134 /// SplitEdge - Split the edge connecting specified block. Pass P must
136 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
138 /// SplitBlock - Split the specified block at the specified instruction - every
139 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
140 /// to a new block. The two blocks are joined by an unconditional branch and
141 /// the loop info is updated.
143 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
145 /// SplitBlockPredecessors - This method transforms BB by introducing a new
146 /// basic block into the function, and moving some of the predecessors of BB to
147 /// be predecessors of the new block. The new predecessors are indicated by the
148 /// Preds array, which has NumPreds elements in it. The new block is given a
149 /// suffix of 'Suffix'. This function returns the new block.
151 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
152 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
153 /// In particular, it does not preserve LoopSimplify (because it's
154 /// complicated to handle the case where one of the edges being split
155 /// is an exit of a loop with other exits).
157 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock*> Preds,
158 const char *Suffix, Pass *P = 0);
160 /// SplitLandingPadPredecessors - This method transforms the landing pad,
161 /// OrigBB, by introducing two new basic blocks into the function. One of those
162 /// new basic blocks gets the predecessors listed in Preds. The other basic
163 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
164 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
165 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
167 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
168 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
169 /// it does not preserve LoopSimplify (because it's complicated to handle the
170 /// case where one of the edges being split is an exit of a loop with other
173 void SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds,
174 const char *Suffix, const char *Suffix2,
175 Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs);
177 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
178 /// instruction into a predecessor which ends in an unconditional branch. If
179 /// the return instruction returns a value defined by a PHI, propagate the
180 /// right value into the return. It returns the new return instruction in the
182 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
185 /// SplitBlockAndInsertIfThen - Split the containing block at the
186 /// specified instruction - everything before and including SplitBefore stays
187 /// in the old basic block, and everything after SplitBefore is moved to a
188 /// new block. The two blocks are connected by a conditional branch
189 /// (with value of Cmp being the condition).
201 /// If Unreachable is true, then ThenBlock ends with
202 /// UnreachableInst, otherwise it branches to Tail.
203 /// Returns the NewBasicBlock's terminator.
204 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
206 MDNode *BranchWeights = 0);
209 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
210 /// but also creates the ElseBlock.
223 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
224 TerminatorInst **ThenTerm,
225 TerminatorInst **ElseTerm,
226 MDNode *BranchWeights = 0);
229 /// GetIfCondition - Check whether BB is the merge point of a if-region.
230 /// If so, return the boolean condition that determines which entry into
231 /// BB will be taken. Also, return by references the block that will be
232 /// entered from if the condition is true, and the block that will be
233 /// entered if the condition is false.
234 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
235 BasicBlock *&IfFalse);
236 } // End llvm namespace