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_BASICBLOCK_H
16 #define LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
18 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
20 #include "llvm/BasicBlock.h"
21 #include "llvm/Support/CFG.h"
30 /// DeleteDeadBlock - Delete the specified block, which must have no
32 void DeleteDeadBlock(BasicBlock *BB);
35 /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
36 /// any single-entry PHI nodes in it, fold them away. This handles the case
37 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
38 /// when the block has exactly one predecessor.
39 void FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0);
41 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
42 /// is dead. Also recursively delete any operands that become dead as
43 /// a result. This includes tracing the def-use list from the PHI to see if
44 /// it is ultimately unused or if it reaches an unused cycle. Return true
45 /// if any PHIs were deleted.
46 bool DeleteDeadPHIs(BasicBlock *BB);
48 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
49 /// if possible. The return value indicates success or failure.
50 bool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0);
52 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
53 // with a value, then remove and delete the original instruction.
55 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
56 BasicBlock::iterator &BI, Value *V);
58 // ReplaceInstWithInst - Replace the instruction specified by BI with the
59 // instruction specified by I. The original instruction is deleted and BI is
60 // updated to point to the new instruction.
62 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
63 BasicBlock::iterator &BI, Instruction *I);
65 // ReplaceInstWithInst - Replace the instruction specified by From with the
66 // instruction specified by To.
68 void ReplaceInstWithInst(Instruction *From, Instruction *To);
70 /// FindFunctionBackedges - Analyze the specified function to find all of the
71 /// loop backedges in the function and return them. This is a relatively cheap
72 /// (compared to computing dominators and loop info) analysis.
74 /// The output is added to Result, as pairs of <from,to> edge info.
75 void FindFunctionBackedges(const Function &F,
76 SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result);
79 /// GetSuccessorNumber - Search for the specified successor of basic block BB
80 /// and return its position in the terminator instruction's list of
81 /// successors. It is an error to call this with a block that is not a
83 unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ);
85 /// isCriticalEdge - Return true if the specified edge is a critical edge.
86 /// Critical edges are edges from a block with multiple successors to a block
87 /// with multiple predecessors.
89 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
90 bool AllowIdenticalEdges = false);
92 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
93 /// split the critical edge. This will update DominatorTree and
94 /// DominatorFrontier information if it is available, thus calling this pass
95 /// will not invalidate either of them. This returns the new block if the edge
96 /// was split, null otherwise.
98 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
99 /// specified successor will be merged into the same critical edge block.
100 /// This is most commonly interesting with switch instructions, which may
101 /// have many edges to any one destination. This ensures that all edges to that
102 /// dest go to one block instead of each going to a different block, but isn't
103 /// the standard definition of a "critical edge".
105 /// It is invalid to call this function on a critical edge that starts at an
106 /// IndirectBrInst. Splitting these edges will almost always create an invalid
107 /// program because the address of the new block won't be the one that is jumped
110 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
111 Pass *P = 0, bool MergeIdenticalEdges = false);
113 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
115 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
118 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
119 /// false. Otherwise, split all edges between the two blocks and return true.
120 /// This updates all of the same analyses as the other SplitCriticalEdge
121 /// function. If P is specified, it updates the analyses
123 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
124 bool MadeChange = false;
125 TerminatorInst *TI = (*PI)->getTerminator();
126 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
127 if (TI->getSuccessor(i) == Succ)
128 MadeChange |= !!SplitCriticalEdge(TI, i, P);
132 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
133 /// and return true, otherwise return false. This method requires that there be
134 /// an edge between the two blocks. If P is specified, it updates the analyses
136 inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
138 bool MergeIdenticalEdges = false) {
139 TerminatorInst *TI = Src->getTerminator();
142 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
143 if (TI->getSuccessor(i) == Dst)
144 return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
149 /// SplitEdge - Split the edge connecting specified block. Pass P must
151 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
153 /// SplitBlock - Split the specified block at the specified instruction - every
154 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
155 /// to a new block. The two blocks are joined by an unconditional branch and
156 /// the loop info is updated.
158 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
160 /// SplitBlockPredecessors - This method transforms BB by introducing a new
161 /// basic block into the function, and moving some of the predecessors of BB to
162 /// be predecessors of the new block. The new predecessors are indicated by the
163 /// Preds array, which has NumPreds elements in it. The new block is given a
164 /// suffix of 'Suffix'. This function returns the new block.
166 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
167 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
168 /// In particular, it does not preserve LoopSimplify (because it's
169 /// complicated to handle the case where one of the edges being split
170 /// is an exit of a loop with other exits).
172 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
173 unsigned NumPreds, const char *Suffix,
176 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
177 /// instruction into a predecessor which ends in an unconditional branch. If
178 /// the return instruction returns a value defined by a PHI, propagate the
179 /// right value into the return. It returns the new return instruction in the
181 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
184 } // End llvm namespace