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