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