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
/// DeleteDeadBlock - Delete the specified block, which must have no
31
void DeleteDeadBlock(BasicBlock *BB);
34
/// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
35
/// any single-entry PHI nodes in it, fold them away. This handles the case
36
/// when all entries to the PHI nodes in a block are guaranteed equal, such as
37
/// when the block has exactly one predecessor.
38
void FoldSingleEntryPHINodes(BasicBlock *BB);
40
/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
41
/// is dead. Also recursively delete any operands that become dead as
42
/// a result. This includes tracing the def-use list from the PHI to see if
43
/// it is ultimately unused or if it reaches an unused cycle. Return true
44
/// if any PHIs were deleted.
45
bool DeleteDeadPHIs(BasicBlock *BB);
47
/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
48
/// if possible. The return value indicates success or failure.
49
bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);
51
// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
52
// with a value, then remove and delete the original instruction.
54
void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
55
BasicBlock::iterator &BI, Value *V);
57
// ReplaceInstWithInst - Replace the instruction specified by BI with the
58
// instruction specified by I. The original instruction is deleted and BI is
59
// updated to point to the new instruction.
61
void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
62
BasicBlock::iterator &BI, Instruction *I);
64
// ReplaceInstWithInst - Replace the instruction specified by From with the
65
// instruction specified by To.
67
void ReplaceInstWithInst(Instruction *From, Instruction *To);
69
/// FindFunctionBackedges - Analyze the specified function to find all of the
70
/// loop backedges in the function and return them. This is a relatively cheap
71
/// (compared to computing dominators and loop info) analysis.
73
/// The output is added to Result, as pairs of <from,to> edge info.
74
void FindFunctionBackedges(const Function &F,
75
SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result);
78
// RemoveSuccessor - Change the specified terminator instruction such that its
79
// successor #SuccNum no longer exists. Because this reduces the outgoing
80
// degree of the current basic block, the actual terminator instruction itself
81
// may have to be changed. In the case where the last successor of the block is
82
// deleted, a return instruction is inserted in its place which can cause a
83
// suprising change in program behavior if it is not expected.
85
void RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum);
87
/// GetSuccessorNumber - Search for the specified successor of basic block BB
88
/// and return its position in the terminator instruction's list of
89
/// successors. It is an error to call this with a block that is not a
91
unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ);
93
/// isCriticalEdge - Return true if the specified edge is a critical edge.
94
/// Critical edges are edges from a block with multiple successors to a block
95
/// with multiple predecessors.
97
bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
98
bool AllowIdenticalEdges = false);
100
/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
101
/// split the critical edge. This will update DominatorTree and
102
/// DominatorFrontier information if it is available, thus calling this pass
103
/// will not invalidate either of them. This returns the new block if the edge
104
/// was split, null otherwise.
106
/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
107
/// specified successor will be merged into the same critical edge block.
108
/// This is most commonly interesting with switch instructions, which may
109
/// have many edges to any one destination. This ensures that all edges to that
110
/// dest go to one block instead of each going to a different block, but isn't
111
/// the standard definition of a "critical edge".
113
/// It is invalid to call this function on a critical edge that starts at an
114
/// IndirectBrInst. Splitting these edges will almost always create an invalid
115
/// program because the address of the new block won't be the one that is jumped
118
BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
119
Pass *P = 0, bool MergeIdenticalEdges = false);
121
inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
123
return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P);
126
/// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
127
/// false. Otherwise, split all edges between the two blocks and return true.
128
/// This updates all of the same analyses as the other SplitCriticalEdge
129
/// function. If P is specified, it updates the analyses
131
inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) {
132
bool MadeChange = false;
133
TerminatorInst *TI = (*PI)->getTerminator();
134
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
135
if (TI->getSuccessor(i) == Succ)
136
MadeChange |= !!SplitCriticalEdge(TI, i, P);
140
/// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
141
/// and return true, otherwise return false. This method requires that there be
142
/// an edge between the two blocks. If P is specified, it updates the analyses
144
inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
146
bool MergeIdenticalEdges = false) {
147
TerminatorInst *TI = Src->getTerminator();
150
assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
151
if (TI->getSuccessor(i) == Dst)
152
return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges);
157
/// SplitEdge - Split the edge connecting specified block. Pass P must
159
BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P);
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.
166
BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P);
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.
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).
180
BasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds,
181
unsigned NumPreds, const char *Suffix,
184
} // End llvm namespace