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