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