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 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
30 /// if possible. The return value indicates success or failure.
31 bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
33 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
34 // with a value, then remove and delete the original instruction.
36 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
37 BasicBlock::iterator &BI, Value *V);
39 // ReplaceInstWithInst - Replace the instruction specified by BI with the
40 // instruction specified by I. The original instruction is deleted and BI is
41 // updated to point to the new instruction.
43 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
44 BasicBlock::iterator &BI, Instruction *I);
46 // ReplaceInstWithInst - Replace the instruction specified by From with the
47 // instruction specified by To.
49 void ReplaceInstWithInst(Instruction *From, Instruction *To);
51 /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the
52 /// instruction before ScanFrom) checking to see if we have the value at the
53 /// memory address *Ptr locally available within a small number of instructions.
54 /// If the value is available, return it.
56 /// If not, return the iterator for the last validated instruction that the
57 /// value would be live through. If we scanned the entire block and didn't find
58 /// something that invalidates *Ptr or provides it, ScanFrom would be left at
59 /// begin() and this returns null. ScanFrom could also be left
61 /// MaxInstsToScan specifies the maximum instructions to scan in the block. If
62 /// it is set to 0, it will scan the whole block. You can also optionally
63 /// specify an alias analysis implementation, which makes this more precise.
64 Value *FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB,
65 BasicBlock::iterator &ScanFrom,
66 unsigned MaxInstsToScan = 6,
67 AliasAnalysis *AA = 0);
71 // RemoveSuccessor - Change the specified terminator instruction such that its
72 // successor #SuccNum no longer exists. Because this reduces the outgoing
73 // degree of the current basic block, the actual terminator instruction itself
74 // may have to be changed. In the case where the last successor of the block is
75 // deleted, a return instruction is inserted in its place which can cause a
76 // suprising change in program behavior if it is not expected.
78 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum);
80 /// isCriticalEdge - Return true if the specified edge is a critical edge.
81 /// Critical edges are edges from a block with multiple successors to a block
82 /// with multiple predecessors.
84 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
85 bool AllowIdenticalEdges = false);
87 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
88 /// split the critical edge. This will update DominatorTree and
89 /// DominatorFrontier information if it is available, thus calling this pass
90 /// will not invalidate either of them. This returns true if the edge was split,
93 /// If MergeIdenticalEdges is true (the default), *all* edges from TI to the
94 /// specified successor will be merged into the same critical edge block.
95 /// This is most commonly interesting with switch instructions, which may
96 /// have many edges to any one destination. This ensures that all edges to that
97 /// dest go to one block instead of each going to a different block, but isn't
98 /// the standard definition of a "critical edge".
100 bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P = 0,
101 bool MergeIdenticalEdges = false);
103 inline bool SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, Pass *P = 0) {
104 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
107 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
108 /// false. Otherwise, split all edges between the two blocks and return true.
109 /// This updates all of the same analyses as the other SplitCriticalEdge
110 /// function. If P is specified, it updates the analyses
112 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
113 bool MadeChange = false;
114 TerminatorInst *TI = (*PI)->getTerminator();
115 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
116 if (TI->getSuccessor(i) == Succ)
117 MadeChange |= SplitCriticalEdge(TI, i, P);
121 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
122 /// and return true, otherwise return false. This method requires that there be
123 /// an edge between the two blocks. If P is specified, it updates the analyses
125 inline bool SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, Pass *P = 0,
126 bool MergeIdenticalEdges = false) {
127 TerminatorInst *TI = Src->getTerminator();
130 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
131 if (TI->getSuccessor(i) == Dst)
132 return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
137 /// SplitEdge - Split the edge connecting specified block. Pass P must
139 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
141 /// SplitBlock - Split the specified block at the specified instruction - every
142 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
143 /// to a new block. The two blocks are joined by an unconditional branch and
144 /// the loop info is updated.
146 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
148 /// SplitBlockPredecessors - This method transforms BB by introducing a new
149 /// basic block into the function, and moving some of the predecessors of BB to
150 /// be predecessors of the new block. The new predecessors are indicated by the
151 /// Preds array, which has NumPreds elements in it. The new block is given a
152 /// suffix of 'Suffix'. This function returns the new block.
154 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and
155 /// DominanceFrontier, but no other analyses.
156 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
157 unsigned NumPreds, const char *Suffix,
160 } // End llvm namespace