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