1 //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions perform manipulations on basic blocks, and
11 // instructions contained within basic blocks.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
16 #define LLVM_TRANSFORMS_UTILS_BASICBLOCK_H
18 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
20 #include "llvm/BasicBlock.h"
21 #include "llvm/Support/CFG.h"
29 /// DeleteBlockIfDead - If the specified basic block is trivially dead (has no
30 /// predecessors and not the entry block), delete it and return true. Otherwise
32 bool DeleteBlockIfDead(BasicBlock *BB);
34 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
35 /// if possible. The return value indicates success or failure.
36 bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
38 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
39 // with a value, then remove and delete the original instruction.
41 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
42 BasicBlock::iterator &BI, Value *V);
44 // ReplaceInstWithInst - Replace the instruction specified by BI with the
45 // instruction specified by I. The original instruction is deleted and BI is
46 // updated to point to the new instruction.
48 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
49 BasicBlock::iterator &BI, Instruction *I);
51 // ReplaceInstWithInst - Replace the instruction specified by From with the
52 // instruction specified by To.
54 void ReplaceInstWithInst(Instruction *From, Instruction *To);
56 /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the
57 /// instruction before ScanFrom) checking to see if we have the value at the
58 /// memory address *Ptr locally available within a small number of instructions.
59 /// If the value is available, return it.
61 /// If not, return the iterator for the last validated instruction that the
62 /// value would be live through. If we scanned the entire block and didn't find
63 /// something that invalidates *Ptr or provides it, ScanFrom would be left at
64 /// begin() and this returns null. ScanFrom could also be left
66 /// MaxInstsToScan specifies the maximum instructions to scan in the block. If
67 /// it is set to 0, it will scan the whole block. You can also optionally
68 /// specify an alias analysis implementation, which makes this more precise.
69 Value *FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
70 BasicBlock::iterator &ScanFrom,
71 unsigned MaxInstsToScan = 6,
72 AliasAnalysis *AA = 0);
76 // RemoveSuccessor - Change the specified terminator instruction such that its
77 // successor #SuccNum no longer exists. Because this reduces the outgoing
78 // degree of the current basic block, the actual terminator instruction itself
79 // may have to be changed. In the case where the last successor of the block is
80 // deleted, a return instruction is inserted in its place which can cause a
81 // suprising change in program behavior if it is not expected.
83 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum);
85 /// isCriticalEdge - Return true if the specified edge is a critical edge.
86 /// Critical edges are edges from a block with multiple successors to a block
87 /// with multiple predecessors.
89 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
90 bool AllowIdenticalEdges = false);
92 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
93 /// split the critical edge. This will update DominatorTree and
94 /// DominatorFrontier information if it is available, thus calling this pass
95 /// will not invalidate either of them. This returns true if the edge was split,
98 /// If MergeIdenticalEdges is true (the default), *all* edges from TI to the
99 /// specified successor will be merged into the same critical edge block.
100 /// This is most commonly interesting with switch instructions, which may
101 /// have many edges to any one destination. This ensures that all edges to that
102 /// dest go to one block instead of each going to a different block, but isn't
103 /// the standard definition of a "critical edge".
105 bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P = 0,
106 bool MergeIdenticalEdges = false);
108 inline bool SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, Pass *P = 0) {
109 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
112 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
113 /// false. Otherwise, split all edges between the two blocks and return true.
114 /// This updates all of the same analyses as the other SplitCriticalEdge
115 /// function. If P is specified, it updates the analyses
117 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
118 bool MadeChange = false;
119 TerminatorInst *TI = (*PI)->getTerminator();
120 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
121 if (TI->getSuccessor(i) == Succ)
122 MadeChange |= SplitCriticalEdge(TI, i, P);
126 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
127 /// and return true, otherwise return false. This method requires that there be
128 /// an edge between the two blocks. If P is specified, it updates the analyses
130 inline bool SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, Pass *P = 0,
131 bool MergeIdenticalEdges = false) {
132 TerminatorInst *TI = Src->getTerminator();
135 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
136 if (TI->getSuccessor(i) == Dst)
137 return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
142 /// SplitEdge - Split the edge connecting specified block. Pass P must
144 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
146 /// SplitBlock - Split the specified block at the specified instruction - every
147 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
148 /// to a new block. The two blocks are joined by an unconditional branch and
149 /// the loop info is updated.
151 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
153 /// SplitBlockPredecessors - This method transforms BB by introducing a new
154 /// basic block into the function, and moving some of the predecessors of BB to
155 /// be predecessors of the new block. The new predecessors are indicated by the
156 /// Preds array, which has NumPreds elements in it. The new block is given a
157 /// suffix of 'Suffix'. This function returns the new block.
159 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and
160 /// DominanceFrontier, but no other analyses.
161 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
162 unsigned NumPreds, const char *Suffix,
165 } // End llvm namespace