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