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