add option to isCriticalEdge
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 DominatorSet, ImmediateDominator,
65 /// DominatorTree, and DominatorFrontier information if it is available, thus
66 /// calling this pass will not invalidate either of them.  This returns true if
67 /// the edge was split, false otherwise.  If MergeIdenticalEdges is true (the
68 /// default), *all* edges from TI to the specified successor will be merged into
69 /// the same critical edge block.  This is most commonly interesting with switch
70 /// instructions, which may have many edges to any one destination.  This
71 /// ensures that all edges to that dest go to one block instead of each going to
72 /// a different block, but isn't the standard definition of a "critical edge".
73 ///
74 bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P = 0,
75                        bool MergeIdenticalEdges = false);
76
77 inline bool SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, Pass *P = 0) {
78   return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
79 }
80
81 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
82 /// false.  Otherwise, split all edges between the two blocks and return true.
83 /// This updates all of the same analyses as the other SplitCriticalEdge
84 /// function.  If P is specified, it updates the analyses
85 /// described above.
86 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
87   bool MadeChange = false;
88   TerminatorInst *TI = (*PI)->getTerminator();
89   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
90     if (TI->getSuccessor(i) == Succ)
91       MadeChange |= SplitCriticalEdge(TI, i, P);
92   return MadeChange;
93 }
94
95 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
96 /// and return true, otherwise return false.  This method requires that there be
97 /// an edge between the two blocks.  If P is specified, it updates the analyses
98 /// described above.
99 inline bool SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, Pass *P = 0,
100                               bool MergeIdenticalEdges = false) {
101   TerminatorInst *TI = Src->getTerminator();
102   unsigned i = 0;
103   while (1) {
104     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
105     if (TI->getSuccessor(i) == Dst)
106       return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
107     ++i;
108   }
109 }
110 } // End llvm namespace
111
112 #endif