Move all of the header files which are involved in modelling the LLVM IR
[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/IR/BasicBlock.h"
21 #include "llvm/Support/CFG.h"
22 #include "llvm/Support/DebugLoc.h"
23
24 namespace llvm {
25
26 class AliasAnalysis;
27 class Instruction;
28 class MDNode;
29 class Pass;
30 class ReturnInst;
31 class TargetLibraryInfo;
32 class TerminatorInst;
33
34 /// DeleteDeadBlock - Delete the specified block, which must have no
35 /// predecessors.
36 void DeleteDeadBlock(BasicBlock *BB);
37
38
39 /// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are
40 /// any single-entry PHI nodes in it, fold them away.  This handles the case
41 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
42 /// when the block has exactly one predecessor.
43 void FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0);
44
45 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
46 /// is dead. Also recursively delete any operands that become dead as
47 /// a result. This includes tracing the def-use list from the PHI to see if
48 /// it is ultimately unused or if it reaches an unused cycle. Return true
49 /// if any PHIs were deleted.
50 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = 0);
51
52 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
53 /// if possible.  The return value indicates success or failure.
54 bool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0);
55
56 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
57 // with a value, then remove and delete the original instruction.
58 //
59 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
60                           BasicBlock::iterator &BI, Value *V);
61
62 // ReplaceInstWithInst - Replace the instruction specified by BI with the
63 // instruction specified by I.  The original instruction is deleted and BI is
64 // updated to point to the new instruction.
65 //
66 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
67                          BasicBlock::iterator &BI, Instruction *I);
68
69 // ReplaceInstWithInst - Replace the instruction specified by From with the
70 // instruction specified by To.
71 //
72 void ReplaceInstWithInst(Instruction *From, Instruction *To);
73
74 /// FindFunctionBackedges - Analyze the specified function to find all of the
75 /// loop backedges in the function and return them.  This is a relatively cheap
76 /// (compared to computing dominators and loop info) analysis.
77 ///
78 /// The output is added to Result, as pairs of <from,to> edge info.
79 void FindFunctionBackedges(const Function &F,
80       SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result);
81
82
83 /// GetSuccessorNumber - Search for the specified successor of basic block BB
84 /// and return its position in the terminator instruction's list of
85 /// successors.  It is an error to call this with a block that is not a
86 /// successor.
87 unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ);
88
89 /// isCriticalEdge - Return true if the specified edge is a critical edge.
90 /// Critical edges are edges from a block with multiple successors to a block
91 /// with multiple predecessors.
92 ///
93 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
94                     bool AllowIdenticalEdges = false);
95
96 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
97 /// split the critical edge.  This will update DominatorTree and
98 /// DominatorFrontier information if it is available, thus calling this pass
99 /// will not invalidate either of them. This returns the new block if the edge
100 /// was split, null otherwise.
101 ///
102 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
103 /// specified successor will be merged into the same critical edge block.
104 /// This is most commonly interesting with switch instructions, which may
105 /// have many edges to any one destination.  This ensures that all edges to that
106 /// dest go to one block instead of each going to a different block, but isn't
107 /// the standard definition of a "critical edge".
108 ///
109 /// It is invalid to call this function on a critical edge that starts at an
110 /// IndirectBrInst.  Splitting these edges will almost always create an invalid
111 /// program because the address of the new block won't be the one that is jumped
112 /// to.
113 ///
114 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
115                               Pass *P = 0, bool MergeIdenticalEdges = false,
116                               bool DontDeleteUselessPHIs = false,
117                               bool SplitLandingPads = false);
118
119 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
120                                      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 BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
143                                      Pass *P = 0,
144                                      bool MergeIdenticalEdges = false,
145                                      bool DontDeleteUselessPHIs = false) {
146   TerminatorInst *TI = Src->getTerminator();
147   unsigned i = 0;
148   while (1) {
149     assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
150     if (TI->getSuccessor(i) == Dst)
151       return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges,
152                                DontDeleteUselessPHIs);
153     ++i;
154   }
155 }
156
157 /// SplitEdge -  Split the edge connecting specified block. Pass P must
158 /// not be NULL.
159 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
160
161 /// SplitBlock - Split the specified block at the specified instruction - every
162 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
163 /// to a new block.  The two blocks are joined by an unconditional branch and
164 /// the loop info is updated.
165 ///
166 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
167
168 /// SplitBlockPredecessors - This method transforms BB by introducing a new
169 /// basic block into the function, and moving some of the predecessors of BB to
170 /// be predecessors of the new block.  The new predecessors are indicated by the
171 /// Preds array, which has NumPreds elements in it.  The new block is given a
172 /// suffix of 'Suffix'.  This function returns the new block.
173 ///
174 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
175 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
176 /// In particular, it does not preserve LoopSimplify (because it's
177 /// complicated to handle the case where one of the edges being split
178 /// is an exit of a loop with other exits).
179 ///
180 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock*> Preds,
181                                    const char *Suffix, Pass *P = 0);
182
183 /// SplitLandingPadPredecessors - This method transforms the landing pad,
184 /// OrigBB, by introducing two new basic blocks into the function. One of those
185 /// new basic blocks gets the predecessors listed in Preds. The other basic
186 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
187 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
188 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
189 ///
190 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
191 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
192 /// it does not preserve LoopSimplify (because it's complicated to handle the
193 /// case where one of the edges being split is an exit of a loop with other
194 /// exits).
195 ///
196 void SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds,
197                                  const char *Suffix, const char *Suffix2,
198                                  Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs);
199
200 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
201 /// instruction into a predecessor which ends in an unconditional branch. If
202 /// the return instruction returns a value defined by a PHI, propagate the
203 /// right value into the return. It returns the new return instruction in the
204 /// predecessor.
205 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
206                                        BasicBlock *Pred);
207
208 /// SplitBlockAndInsertIfThen - Split the containing block at the
209 /// specified instruction - everything before and including Cmp stays
210 /// in the old basic block, and everything after Cmp is moved to a
211 /// new block. The two blocks are connected by a conditional branch
212 /// (with value of Cmp being the condition).
213 /// Before:
214 ///   Head
215 ///   Cmp
216 ///   Tail
217 /// After:
218 ///   Head
219 ///   Cmp
220 ///   if (Cmp)
221 ///     ThenBlock
222 ///   Tail
223 ///
224 /// If Unreachable is true, then ThenBlock ends with
225 /// UnreachableInst, otherwise it branches to Tail.
226 /// Returns the NewBasicBlock's terminator.
227
228 TerminatorInst *SplitBlockAndInsertIfThen(Instruction *Cmp,
229     bool Unreachable, MDNode *BranchWeights = 0);
230
231 } // End llvm namespace
232
233 #endif