Refactor to share code to find the position of a basic block successor in the
[oota-llvm.git] / include / llvm / Transforms / Utils / BasicBlockUtils.h
1 //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions perform manipulations on basic blocks, and
11 // instructions contained within basic blocks.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
16 #define LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
17
18 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
19
20 #include "llvm/BasicBlock.h"
21 #include "llvm/Support/CFG.h"
22
23 namespace llvm {
24
25 class Instruction;
26 class Pass;
27 class AliasAnalysis;
28
29 /// DeleteDeadBlock - Delete the specified block, which must have no
30 /// predecessors.
31 void DeleteDeadBlock(BasicBlock *BB);
32   
33   
34 /// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are
35 /// any single-entry PHI nodes in it, fold them away.  This handles the case
36 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
37 /// when the block has exactly one predecessor.
38 void FoldSingleEntryPHINodes(BasicBlock *BB);
39
40 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
41 /// is dead. Also recursively delete any operands that become dead as
42 /// a result. This includes tracing the def-use list from the PHI to see if
43 /// it is ultimately unused or if it reaches an unused cycle. Return true
44 /// if any PHIs were deleted.
45 bool DeleteDeadPHIs(BasicBlock *BB);
46
47 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
48 /// if possible.  The return value indicates success or failure.
49 bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
50
51 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
52 // with a value, then remove and delete the original instruction.
53 //
54 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
55                           BasicBlock::iterator &BI, Value *V);
56
57 // ReplaceInstWithInst - Replace the instruction specified by BI with the
58 // instruction specified by I.  The original instruction is deleted and BI is
59 // updated to point to the new instruction.
60 //
61 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
62                          BasicBlock::iterator &BI, Instruction *I);
63
64 // ReplaceInstWithInst - Replace the instruction specified by From with the
65 // instruction specified by To.
66 //
67 void ReplaceInstWithInst(Instruction *From, Instruction *To);
68
69 /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the
70 /// instruction before ScanFrom) checking to see if we have the value at the
71 /// memory address *Ptr locally available within a small number of instructions.
72 /// If the value is available, return it.
73 ///
74 /// If not, return the iterator for the last validated instruction that the 
75 /// value would be live through.  If we scanned the entire block and didn't find
76 /// something that invalidates *Ptr or provides it, ScanFrom would be left at
77 /// begin() and this returns null.  ScanFrom could also be left 
78 ///
79 /// MaxInstsToScan specifies the maximum instructions to scan in the block.  If
80 /// it is set to 0, it will scan the whole block. You can also optionally
81 /// specify an alias analysis implementation, which makes this more precise.
82 Value *FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
83                                 BasicBlock::iterator &ScanFrom,
84                                 unsigned MaxInstsToScan = 6,
85                                 AliasAnalysis *AA = 0);
86
87 /// FindFunctionBackedges - Analyze the specified function to find all of the
88 /// loop backedges in the function and return them.  This is a relatively cheap
89 /// (compared to computing dominators and loop info) analysis.
90 ///
91 /// The output is added to Result, as pairs of <from,to> edge info.
92 void FindFunctionBackedges(const Function &F,
93       SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result);
94   
95
96 // RemoveSuccessor - Change the specified terminator instruction such that its
97 // successor #SuccNum no longer exists.  Because this reduces the outgoing
98 // degree of the current basic block, the actual terminator instruction itself
99 // may have to be changed.  In the case where the last successor of the block is
100 // deleted, a return instruction is inserted in its place which can cause a
101 // suprising change in program behavior if it is not expected.
102 //
103 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum);
104
105 /// SuccessorNumber - Search for the specified successor of basic block BB and
106 /// return its position in the terminator instruction's list of successors.
107 /// It is an error to call this with a block that is not a successor.
108 unsigned SuccessorNumber(BasicBlock *BB, BasicBlock *Succ);
109
110 /// isCriticalEdge - Return true if the specified edge is a critical edge.
111 /// Critical edges are edges from a block with multiple successors to a block
112 /// with multiple predecessors.
113 ///
114 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
115                     bool AllowIdenticalEdges = false);
116
117 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
118 /// split the critical edge.  This will update DominatorTree and
119 /// DominatorFrontier information if it is available, thus calling this pass
120 /// will not invalidate either of them. This returns the new block if the edge
121 /// was split, null otherwise.
122 ///
123 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
124 /// specified successor will be merged into the same critical edge block.  
125 /// This is most commonly interesting with switch instructions, which may 
126 /// have many edges to any one destination.  This ensures that all edges to that
127 /// dest go to one block instead of each going to a different block, but isn't 
128 /// the standard definition of a "critical edge".
129 ///
130 /// It is invalid to call this function on a critical edge that starts at an
131 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
132 /// program because the address of the new block won't be the one that is jumped
133 /// to.
134 ///
135 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
136                               Pass *P = 0, bool MergeIdenticalEdges = false);
137
138 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
139                                      Pass *P = 0) {
140   return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
141 }
142
143 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
144 /// false.  Otherwise, split all edges between the two blocks and return true.
145 /// This updates all of the same analyses as the other SplitCriticalEdge
146 /// function.  If P is specified, it updates the analyses
147 /// described above.
148 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
149   bool MadeChange = false;
150   TerminatorInst *TI = (*PI)->getTerminator();
151   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
152     if (TI->getSuccessor(i) == Succ)
153       MadeChange |= !!SplitCriticalEdge(TI, i, P);
154   return MadeChange;
155 }
156
157 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
158 /// and return true, otherwise return false.  This method requires that there be
159 /// an edge between the two blocks.  If P is specified, it updates the analyses
160 /// described above.
161 inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
162                                      Pass *P = 0,
163                                      bool MergeIdenticalEdges = false) {
164   TerminatorInst *TI = Src->getTerminator();
165   unsigned i = 0;
166   while (1) {
167     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
168     if (TI->getSuccessor(i) == Dst)
169       return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
170     ++i;
171   }
172 }
173
174 /// SplitEdge -  Split the edge connecting specified block. Pass P must 
175 /// not be NULL. 
176 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
177
178 /// SplitBlock - Split the specified block at the specified instruction - every
179 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
180 /// to a new block.  The two blocks are joined by an unconditional branch and
181 /// the loop info is updated.
182 ///
183 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
184  
185 /// SplitBlockPredecessors - This method transforms BB by introducing a new
186 /// basic block into the function, and moving some of the predecessors of BB to
187 /// be predecessors of the new block.  The new predecessors are indicated by the
188 /// Preds array, which has NumPreds elements in it.  The new block is given a
189 /// suffix of 'Suffix'.  This function returns the new block.
190 ///
191 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
192 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
193 /// In particular, it does not preserve LoopSimplify (because it's
194 /// complicated to handle the case where one of the edges being split
195 /// is an exit of a loop with other exits).
196 ///
197 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
198                                    unsigned NumPreds, const char *Suffix,
199                                    Pass *P = 0);
200   
201 } // End llvm namespace
202
203 #endif