BasicBlockUtils.h revision 218893
1193323Sed//===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This family of functions perform manipulations on basic blocks, and 11193323Sed// instructions contained within basic blocks. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCK_H 16193323Sed#define LLVM_TRANSFORMS_UTILS_BASICBLOCK_H 17193323Sed 18193323Sed// FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock 19193323Sed 20193323Sed#include "llvm/BasicBlock.h" 21193323Sed#include "llvm/Support/CFG.h" 22193323Sed 23193323Sednamespace llvm { 24193323Sed 25218893Sdimclass AliasAnalysis; 26193323Sedclass Instruction; 27193323Sedclass Pass; 28218893Sdimclass ReturnInst; 29193323Sed 30193323Sed/// DeleteDeadBlock - Delete the specified block, which must have no 31193323Sed/// predecessors. 32193323Sedvoid DeleteDeadBlock(BasicBlock *BB); 33193323Sed 34193323Sed 35193323Sed/// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are 36193323Sed/// any single-entry PHI nodes in it, fold them away. This handles the case 37193323Sed/// when all entries to the PHI nodes in a block are guaranteed equal, such as 38193323Sed/// when the block has exactly one predecessor. 39218893Sdimvoid FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0); 40193323Sed 41193323Sed/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it 42193323Sed/// is dead. Also recursively delete any operands that become dead as 43193323Sed/// a result. This includes tracing the def-use list from the PHI to see if 44202375Srdivacky/// it is ultimately unused or if it reaches an unused cycle. Return true 45202375Srdivacky/// if any PHIs were deleted. 46202375Srdivackybool DeleteDeadPHIs(BasicBlock *BB); 47193323Sed 48193323Sed/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, 49193323Sed/// if possible. The return value indicates success or failure. 50218893Sdimbool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0); 51193323Sed 52193323Sed// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) 53193323Sed// with a value, then remove and delete the original instruction. 54193323Sed// 55193323Sedvoid ReplaceInstWithValue(BasicBlock::InstListType &BIL, 56193323Sed BasicBlock::iterator &BI, Value *V); 57193323Sed 58193323Sed// ReplaceInstWithInst - Replace the instruction specified by BI with the 59193323Sed// instruction specified by I. The original instruction is deleted and BI is 60193323Sed// updated to point to the new instruction. 61193323Sed// 62193323Sedvoid ReplaceInstWithInst(BasicBlock::InstListType &BIL, 63193323Sed BasicBlock::iterator &BI, Instruction *I); 64193323Sed 65193323Sed// ReplaceInstWithInst - Replace the instruction specified by From with the 66193323Sed// instruction specified by To. 67193323Sed// 68193323Sedvoid ReplaceInstWithInst(Instruction *From, Instruction *To); 69193323Sed 70193323Sed/// FindFunctionBackedges - Analyze the specified function to find all of the 71193323Sed/// loop backedges in the function and return them. This is a relatively cheap 72193323Sed/// (compared to computing dominators and loop info) analysis. 73193323Sed/// 74193323Sed/// The output is added to Result, as pairs of <from,to> edge info. 75193323Sedvoid FindFunctionBackedges(const Function &F, 76193323Sed SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result); 77193323Sed 78193323Sed 79204642Srdivacky/// GetSuccessorNumber - Search for the specified successor of basic block BB 80204642Srdivacky/// and return its position in the terminator instruction's list of 81204642Srdivacky/// successors. It is an error to call this with a block that is not a 82204642Srdivacky/// successor. 83204642Srdivackyunsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ); 84204642Srdivacky 85193323Sed/// isCriticalEdge - Return true if the specified edge is a critical edge. 86193323Sed/// Critical edges are edges from a block with multiple successors to a block 87193323Sed/// with multiple predecessors. 88193323Sed/// 89193323Sedbool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, 90193323Sed bool AllowIdenticalEdges = false); 91193323Sed 92193323Sed/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to 93193323Sed/// split the critical edge. This will update DominatorTree and 94193323Sed/// DominatorFrontier information if it is available, thus calling this pass 95198892Srdivacky/// will not invalidate either of them. This returns the new block if the edge 96198892Srdivacky/// was split, null otherwise. 97193323Sed/// 98193323Sed/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the 99193323Sed/// specified successor will be merged into the same critical edge block. 100193323Sed/// This is most commonly interesting with switch instructions, which may 101193323Sed/// have many edges to any one destination. This ensures that all edges to that 102193323Sed/// dest go to one block instead of each going to a different block, but isn't 103193323Sed/// the standard definition of a "critical edge". 104193323Sed/// 105198892Srdivacky/// It is invalid to call this function on a critical edge that starts at an 106198892Srdivacky/// IndirectBrInst. Splitting these edges will almost always create an invalid 107198892Srdivacky/// program because the address of the new block won't be the one that is jumped 108198892Srdivacky/// to. 109198892Srdivacky/// 110198090SrdivackyBasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, 111198090Srdivacky Pass *P = 0, bool MergeIdenticalEdges = false); 112193323Sed 113198892Srdivackyinline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, 114198892Srdivacky Pass *P = 0) { 115193323Sed return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P); 116193323Sed} 117193323Sed 118193323Sed/// SplitCriticalEdge - If the edge from *PI to BB is not critical, return 119193323Sed/// false. Otherwise, split all edges between the two blocks and return true. 120193323Sed/// This updates all of the same analyses as the other SplitCriticalEdge 121193323Sed/// function. If P is specified, it updates the analyses 122193323Sed/// described above. 123193323Sedinline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) { 124193323Sed bool MadeChange = false; 125193323Sed TerminatorInst *TI = (*PI)->getTerminator(); 126193323Sed for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 127193323Sed if (TI->getSuccessor(i) == Succ) 128198090Srdivacky MadeChange |= !!SplitCriticalEdge(TI, i, P); 129193323Sed return MadeChange; 130193323Sed} 131193323Sed 132193323Sed/// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge 133193323Sed/// and return true, otherwise return false. This method requires that there be 134193323Sed/// an edge between the two blocks. If P is specified, it updates the analyses 135193323Sed/// described above. 136198090Srdivackyinline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 137198090Srdivacky Pass *P = 0, 138198090Srdivacky bool MergeIdenticalEdges = false) { 139193323Sed TerminatorInst *TI = Src->getTerminator(); 140193323Sed unsigned i = 0; 141193323Sed while (1) { 142193323Sed assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 143193323Sed if (TI->getSuccessor(i) == Dst) 144193323Sed return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges); 145193323Sed ++i; 146193323Sed } 147193323Sed} 148193323Sed 149193323Sed/// SplitEdge - Split the edge connecting specified block. Pass P must 150193323Sed/// not be NULL. 151193323SedBasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P); 152193323Sed 153193323Sed/// SplitBlock - Split the specified block at the specified instruction - every 154193323Sed/// thing before SplitPt stays in Old and everything starting with SplitPt moves 155193323Sed/// to a new block. The two blocks are joined by an unconditional branch and 156193323Sed/// the loop info is updated. 157193323Sed/// 158193323SedBasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P); 159193323Sed 160193323Sed/// SplitBlockPredecessors - This method transforms BB by introducing a new 161193323Sed/// basic block into the function, and moving some of the predecessors of BB to 162193323Sed/// be predecessors of the new block. The new predecessors are indicated by the 163193323Sed/// Preds array, which has NumPreds elements in it. The new block is given a 164193323Sed/// suffix of 'Suffix'. This function returns the new block. 165193323Sed/// 166198090Srdivacky/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 167198090Srdivacky/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. 168198090Srdivacky/// In particular, it does not preserve LoopSimplify (because it's 169198090Srdivacky/// complicated to handle the case where one of the edges being split 170198090Srdivacky/// is an exit of a loop with other exits). 171198090Srdivacky/// 172193323SedBasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds, 173193323Sed unsigned NumPreds, const char *Suffix, 174193323Sed Pass *P = 0); 175218893Sdim 176218893Sdim/// FoldReturnIntoUncondBranch - This method duplicates the specified return 177218893Sdim/// instruction into a predecessor which ends in an unconditional branch. If 178218893Sdim/// the return instruction returns a value defined by a PHI, propagate the 179218893Sdim/// right value into the return. It returns the new return instruction in the 180218893Sdim/// predecessor. 181218893SdimReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 182218893Sdim BasicBlock *Pred); 183218893Sdim 184193323Sed} // End llvm namespace 185193323Sed 186193323Sed#endif 187