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