Factor MergeBlockIntoPredecessor out into BasicBlockUtils.
[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
28 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
29 /// if possible.  The return value indicates success or failure.
30 bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P);
31
32 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
33 // with a value, then remove and delete the original instruction.
34 //
35 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
36                           BasicBlock::iterator &BI, Value *V);
37
38 // ReplaceInstWithInst - Replace the instruction specified by BI with the
39 // instruction specified by I.  The original instruction is deleted and BI is
40 // updated to point to the new instruction.
41 //
42 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
43                          BasicBlock::iterator &BI, Instruction *I);
44
45 // ReplaceInstWithInst - Replace the instruction specified by From with the
46 // instruction specified by To.
47 //
48 void ReplaceInstWithInst(Instruction *From, Instruction *To);
49
50
51 // RemoveSuccessor - Change the specified terminator instruction such that its
52 // successor #SuccNum no longer exists.  Because this reduces the outgoing
53 // degree of the current basic block, the actual terminator instruction itself
54 // may have to be changed.  In the case where the last successor of the block is
55 // deleted, a return instruction is inserted in its place which can cause a
56 // suprising change in program behavior if it is not expected.
57 //
58 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum);
59
60 /// isCriticalEdge - Return true if the specified edge is a critical edge.
61 /// Critical edges are edges from a block with multiple successors to a block
62 /// with multiple predecessors.
63 ///
64 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
65                     bool AllowIdenticalEdges = false);
66
67 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
68 /// split the critical edge.  This will update DominatorTree and
69 /// DominatorFrontier information if it is available, thus calling this pass
70 /// will not invalidate either of them. This returns true if the edge was split,
71 /// false otherwise.  
72 ///
73 /// If MergeIdenticalEdges is true (the default), *all* edges from TI to the 
74 /// specified successor will be merged into the same critical edge block.  
75 /// This is most commonly interesting with switch instructions, which may 
76 /// have many edges to any one destination.  This ensures that all edges to that 
77 /// dest go to one block instead of each going to a different block, but isn't 
78 /// the standard definition of a "critical edge".
79 ///
80 bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P = 0,
81                        bool MergeIdenticalEdges = false);
82
83 inline bool SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, Pass *P = 0) {
84   return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
85 }
86
87 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
88 /// false.  Otherwise, split all edges between the two blocks and return true.
89 /// This updates all of the same analyses as the other SplitCriticalEdge
90 /// function.  If P is specified, it updates the analyses
91 /// described above.
92 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
93   bool MadeChange = false;
94   TerminatorInst *TI = (*PI)->getTerminator();
95   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
96     if (TI->getSuccessor(i) == Succ)
97       MadeChange |= SplitCriticalEdge(TI, i, P);
98   return MadeChange;
99 }
100
101 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
102 /// and return true, otherwise return false.  This method requires that there be
103 /// an edge between the two blocks.  If P is specified, it updates the analyses
104 /// described above.
105 inline bool SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, Pass *P = 0,
106                               bool MergeIdenticalEdges = false) {
107   TerminatorInst *TI = Src->getTerminator();
108   unsigned i = 0;
109   while (1) {
110     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
111     if (TI->getSuccessor(i) == Dst)
112       return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
113     ++i;
114   }
115 }
116
117 /// SplitEdge -  Split the edge connecting specified block. Pass P must 
118 /// not be NULL. 
119 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
120
121 /// SplitBlock - Split the specified block at the specified instruction - every
122 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
123 /// to a new block.  The two blocks are joined by an unconditional branch and
124 /// the loop info is updated.
125 ///
126 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
127  
128 /// SplitBlockPredecessors - This method transforms BB by introducing a new
129 /// basic block into the function, and moving some of the predecessors of BB to
130 /// be predecessors of the new block.  The new predecessors are indicated by the
131 /// Preds array, which has NumPreds elements in it.  The new block is given a
132 /// suffix of 'Suffix'.  This function returns the new block.
133 ///
134 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and
135 /// DominanceFrontier, but no other analyses.
136 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
137                                    unsigned NumPreds, const char *Suffix,
138                                    Pass *P = 0);
139   
140 } // End llvm namespace
141
142 #endif