BasicBlockUtils.h revision 226633
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" 22221345Sdim#include "llvm/Support/DebugLoc.h" 23193323Sed 24193323Sednamespace llvm { 25193323Sed 26218893Sdimclass AliasAnalysis; 27193323Sedclass Instruction; 28193323Sedclass Pass; 29218893Sdimclass ReturnInst; 30193323Sed 31193323Sed/// DeleteDeadBlock - Delete the specified block, which must have no 32193323Sed/// predecessors. 33193323Sedvoid DeleteDeadBlock(BasicBlock *BB); 34226633Sdim 35226633Sdim 36193323Sed/// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are 37193323Sed/// any single-entry PHI nodes in it, fold them away. This handles the case 38193323Sed/// when all entries to the PHI nodes in a block are guaranteed equal, such as 39193323Sed/// when the block has exactly one predecessor. 40218893Sdimvoid FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0); 41193323Sed 42193323Sed/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it 43193323Sed/// is dead. Also recursively delete any operands that become dead as 44193323Sed/// a result. This includes tracing the def-use list from the PHI to see if 45202375Srdivacky/// it is ultimately unused or if it reaches an unused cycle. Return true 46202375Srdivacky/// if any PHIs were deleted. 47202375Srdivackybool DeleteDeadPHIs(BasicBlock *BB); 48193323Sed 49193323Sed/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, 50193323Sed/// if possible. The return value indicates success or failure. 51218893Sdimbool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0); 52193323Sed 53193323Sed// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) 54193323Sed// with a value, then remove and delete the original instruction. 55193323Sed// 56193323Sedvoid ReplaceInstWithValue(BasicBlock::InstListType &BIL, 57193323Sed BasicBlock::iterator &BI, Value *V); 58193323Sed 59193323Sed// ReplaceInstWithInst - Replace the instruction specified by BI with the 60193323Sed// instruction specified by I. The original instruction is deleted and BI is 61193323Sed// updated to point to the new instruction. 62193323Sed// 63193323Sedvoid ReplaceInstWithInst(BasicBlock::InstListType &BIL, 64193323Sed BasicBlock::iterator &BI, Instruction *I); 65193323Sed 66193323Sed// ReplaceInstWithInst - Replace the instruction specified by From with the 67193323Sed// instruction specified by To. 68193323Sed// 69193323Sedvoid ReplaceInstWithInst(Instruction *From, Instruction *To); 70193323Sed 71193323Sed/// FindFunctionBackedges - Analyze the specified function to find all of the 72193323Sed/// loop backedges in the function and return them. This is a relatively cheap 73193323Sed/// (compared to computing dominators and loop info) analysis. 74193323Sed/// 75193323Sed/// The output is added to Result, as pairs of <from,to> edge info. 76193323Sedvoid FindFunctionBackedges(const Function &F, 77193323Sed SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result); 78193323Sed 79226633Sdim 80204642Srdivacky/// GetSuccessorNumber - Search for the specified successor of basic block BB 81204642Srdivacky/// and return its position in the terminator instruction's list of 82204642Srdivacky/// successors. It is an error to call this with a block that is not a 83204642Srdivacky/// successor. 84204642Srdivackyunsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ); 85204642Srdivacky 86193323Sed/// isCriticalEdge - Return true if the specified edge is a critical edge. 87193323Sed/// Critical edges are edges from a block with multiple successors to a block 88193323Sed/// with multiple predecessors. 89193323Sed/// 90193323Sedbool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, 91193323Sed bool AllowIdenticalEdges = false); 92193323Sed 93193323Sed/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to 94193323Sed/// split the critical edge. This will update DominatorTree and 95193323Sed/// DominatorFrontier information if it is available, thus calling this pass 96198892Srdivacky/// will not invalidate either of them. This returns the new block if the edge 97198892Srdivacky/// was split, null otherwise. 98193323Sed/// 99193323Sed/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the 100226633Sdim/// specified successor will be merged into the same critical edge block. 101226633Sdim/// This is most commonly interesting with switch instructions, which may 102193323Sed/// have many edges to any one destination. This ensures that all edges to that 103226633Sdim/// dest go to one block instead of each going to a different block, but isn't 104193323Sed/// the standard definition of a "critical edge". 105193323Sed/// 106198892Srdivacky/// It is invalid to call this function on a critical edge that starts at an 107198892Srdivacky/// IndirectBrInst. Splitting these edges will almost always create an invalid 108198892Srdivacky/// program because the address of the new block won't be the one that is jumped 109198892Srdivacky/// to. 110198892Srdivacky/// 111198090SrdivackyBasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, 112226633Sdim Pass *P = 0, bool MergeIdenticalEdges = false, 113226633Sdim bool DontDeleteUselessPHIs = false); 114193323Sed 115198892Srdivackyinline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, 116198892Srdivacky Pass *P = 0) { 117193323Sed return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P); 118193323Sed} 119193323Sed 120193323Sed/// SplitCriticalEdge - If the edge from *PI to BB is not critical, return 121193323Sed/// false. Otherwise, split all edges between the two blocks and return true. 122193323Sed/// This updates all of the same analyses as the other SplitCriticalEdge 123193323Sed/// function. If P is specified, it updates the analyses 124193323Sed/// described above. 125193323Sedinline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) { 126193323Sed bool MadeChange = false; 127193323Sed TerminatorInst *TI = (*PI)->getTerminator(); 128193323Sed for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 129193323Sed if (TI->getSuccessor(i) == Succ) 130198090Srdivacky MadeChange |= !!SplitCriticalEdge(TI, i, P); 131193323Sed return MadeChange; 132193323Sed} 133193323Sed 134193323Sed/// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge 135193323Sed/// and return true, otherwise return false. This method requires that there be 136193323Sed/// an edge between the two blocks. If P is specified, it updates the analyses 137193323Sed/// described above. 138198090Srdivackyinline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 139198090Srdivacky Pass *P = 0, 140226633Sdim bool MergeIdenticalEdges = false, 141226633Sdim bool DontDeleteUselessPHIs = false) { 142193323Sed TerminatorInst *TI = Src->getTerminator(); 143193323Sed unsigned i = 0; 144193323Sed while (1) { 145193323Sed assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 146193323Sed if (TI->getSuccessor(i) == Dst) 147226633Sdim return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges, 148226633Sdim DontDeleteUselessPHIs); 149193323Sed ++i; 150193323Sed } 151193323Sed} 152193323Sed 153226633Sdim/// SplitEdge - Split the edge connecting specified block. Pass P must 154226633Sdim/// not be NULL. 155193323SedBasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P); 156193323Sed 157193323Sed/// SplitBlock - Split the specified block at the specified instruction - every 158193323Sed/// thing before SplitPt stays in Old and everything starting with SplitPt moves 159193323Sed/// to a new block. The two blocks are joined by an unconditional branch and 160193323Sed/// the loop info is updated. 161193323Sed/// 162193323SedBasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P); 163226633Sdim 164193323Sed/// SplitBlockPredecessors - This method transforms BB by introducing a new 165193323Sed/// basic block into the function, and moving some of the predecessors of BB to 166193323Sed/// be predecessors of the new block. The new predecessors are indicated by the 167193323Sed/// Preds array, which has NumPreds elements in it. The new block is given a 168193323Sed/// suffix of 'Suffix'. This function returns the new block. 169193323Sed/// 170198090Srdivacky/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 171198090Srdivacky/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. 172198090Srdivacky/// In particular, it does not preserve LoopSimplify (because it's 173198090Srdivacky/// complicated to handle the case where one of the edges being split 174198090Srdivacky/// is an exit of a loop with other exits). 175198090Srdivacky/// 176193323SedBasicBlock *SplitBlockPredecessors(BasicBlock *BB, BasicBlock *const *Preds, 177193323Sed unsigned NumPreds, const char *Suffix, 178193323Sed Pass *P = 0); 179218893Sdim 180226633Sdim/// SplitLandingPadPredecessors - This method transforms the landing pad, 181226633Sdim/// OrigBB, by introducing two new basic blocks into the function. One of those 182226633Sdim/// new basic blocks gets the predecessors listed in Preds. The other basic 183226633Sdim/// block gets the remaining predecessors of OrigBB. The landingpad instruction 184226633Sdim/// OrigBB is clone into both of the new basic blocks. The new blocks are given 185226633Sdim/// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector. 186226633Sdim/// 187226633Sdim/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 188226633Sdim/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular, 189226633Sdim/// it does not preserve LoopSimplify (because it's complicated to handle the 190226633Sdim/// case where one of the edges being split is an exit of a loop with other 191226633Sdim/// exits). 192226633Sdim/// 193226633Sdimvoid SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds, 194226633Sdim const char *Suffix, const char *Suffix2, 195226633Sdim Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs); 196226633Sdim 197218893Sdim/// FoldReturnIntoUncondBranch - This method duplicates the specified return 198218893Sdim/// instruction into a predecessor which ends in an unconditional branch. If 199218893Sdim/// the return instruction returns a value defined by a PHI, propagate the 200218893Sdim/// right value into the return. It returns the new return instruction in the 201218893Sdim/// predecessor. 202218893SdimReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 203218893Sdim BasicBlock *Pred); 204218893Sdim 205226633Sdim/// GetFirstDebugLocInBasicBlock - Return first valid DebugLoc entry in a 206221345Sdim/// given basic block. 207221345SdimDebugLoc GetFirstDebugLocInBasicBlock(const BasicBlock *BB); 208221345Sdim 209193323Sed} // End llvm namespace 210193323Sed 211193323Sed#endif 212