1 //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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"
28 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
29 // with a value, then remove and delete the original instruction.
31 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
32 BasicBlock::iterator &BI, Value *V);
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.
38 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
39 BasicBlock::iterator &BI, Instruction *I);
41 // ReplaceInstWithInst - Replace the instruction specified by From with the
42 // instruction specified by To.
44 void ReplaceInstWithInst(Instruction *From, Instruction *To);
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.
54 void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum);
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.
60 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum);
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.
68 bool SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, Pass *P = 0);
70 inline bool SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, Pass *P = 0) {
71 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
74 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
75 /// false. Otherwise, split all edges between the two blocks and return true.
76 /// This updates all of the same analyses as the other SplitCriticalEdge
78 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
79 bool MadeChange = false;
80 TerminatorInst *TI = (*PI)->getTerminator();
81 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
82 if (TI->getSuccessor(i) == Succ)
83 MadeChange |= SplitCriticalEdge(TI, i, P);
87 inline bool SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, Pass *P = 0) {
88 TerminatorInst *TI = Src->getTerminator();
89 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
90 if (TI->getSuccessor(i) == Dst)
91 return SplitCriticalEdge(TI, i, P);
94 } // End llvm namespace