BasicBlockUtils.h revision 261991
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 15249423Sdim#ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 16249423Sdim#define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 17193323Sed 18193323Sed// FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock 19193323Sed 20249423Sdim#include "llvm/IR/BasicBlock.h" 21193323Sed#include "llvm/Support/CFG.h" 22193323Sed 23193323Sednamespace llvm { 24193323Sed 25218893Sdimclass AliasAnalysis; 26193323Sedclass Instruction; 27243830Sdimclass MDNode; 28193323Sedclass Pass; 29218893Sdimclass ReturnInst; 30243830Sdimclass TargetLibraryInfo; 31243830Sdimclass TerminatorInst; 32193323Sed 33193323Sed/// DeleteDeadBlock - Delete the specified block, which must have no 34193323Sed/// predecessors. 35193323Sedvoid DeleteDeadBlock(BasicBlock *BB); 36226633Sdim 37226633Sdim 38193323Sed/// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are 39193323Sed/// any single-entry PHI nodes in it, fold them away. This handles the case 40193323Sed/// when all entries to the PHI nodes in a block are guaranteed equal, such as 41193323Sed/// when the block has exactly one predecessor. 42218893Sdimvoid FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0); 43193323Sed 44193323Sed/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it 45193323Sed/// is dead. Also recursively delete any operands that become dead as 46193323Sed/// a result. This includes tracing the def-use list from the PHI to see if 47202375Srdivacky/// it is ultimately unused or if it reaches an unused cycle. Return true 48202375Srdivacky/// if any PHIs were deleted. 49243830Sdimbool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = 0); 50193323Sed 51193323Sed/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, 52193323Sed/// if possible. The return value indicates success or failure. 53218893Sdimbool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0); 54193323Sed 55193323Sed// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) 56193323Sed// with a value, then remove and delete the original instruction. 57193323Sed// 58193323Sedvoid ReplaceInstWithValue(BasicBlock::InstListType &BIL, 59193323Sed BasicBlock::iterator &BI, Value *V); 60193323Sed 61193323Sed// ReplaceInstWithInst - Replace the instruction specified by BI with the 62193323Sed// instruction specified by I. The original instruction is deleted and BI is 63193323Sed// updated to point to the new instruction. 64193323Sed// 65193323Sedvoid ReplaceInstWithInst(BasicBlock::InstListType &BIL, 66193323Sed BasicBlock::iterator &BI, Instruction *I); 67193323Sed 68193323Sed// ReplaceInstWithInst - Replace the instruction specified by From with the 69193323Sed// instruction specified by To. 70193323Sed// 71193323Sedvoid ReplaceInstWithInst(Instruction *From, Instruction *To); 72193323Sed 73193323Sed/// SplitCriticalEdge - If this edge is a critical edge, insert a new node to 74193323Sed/// split the critical edge. This will update DominatorTree and 75193323Sed/// DominatorFrontier information if it is available, thus calling this pass 76198892Srdivacky/// will not invalidate either of them. This returns the new block if the edge 77198892Srdivacky/// was split, null otherwise. 78193323Sed/// 79193323Sed/// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the 80226633Sdim/// specified successor will be merged into the same critical edge block. 81226633Sdim/// This is most commonly interesting with switch instructions, which may 82193323Sed/// have many edges to any one destination. This ensures that all edges to that 83226633Sdim/// dest go to one block instead of each going to a different block, but isn't 84193323Sed/// the standard definition of a "critical edge". 85193323Sed/// 86198892Srdivacky/// It is invalid to call this function on a critical edge that starts at an 87198892Srdivacky/// IndirectBrInst. Splitting these edges will almost always create an invalid 88198892Srdivacky/// program because the address of the new block won't be the one that is jumped 89198892Srdivacky/// to. 90198892Srdivacky/// 91198090SrdivackyBasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, 92226633Sdim Pass *P = 0, bool MergeIdenticalEdges = false, 93234982Sdim bool DontDeleteUselessPHIs = false, 94234982Sdim bool SplitLandingPads = false); 95193323Sed 96198892Srdivackyinline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, 97198892Srdivacky Pass *P = 0) { 98193323Sed return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P); 99193323Sed} 100193323Sed 101193323Sed/// SplitCriticalEdge - If the edge from *PI to BB is not critical, return 102193323Sed/// false. Otherwise, split all edges between the two blocks and return true. 103193323Sed/// This updates all of the same analyses as the other SplitCriticalEdge 104193323Sed/// function. If P is specified, it updates the analyses 105193323Sed/// described above. 106193323Sedinline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) { 107193323Sed bool MadeChange = false; 108193323Sed TerminatorInst *TI = (*PI)->getTerminator(); 109193323Sed for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 110193323Sed if (TI->getSuccessor(i) == Succ) 111198090Srdivacky MadeChange |= !!SplitCriticalEdge(TI, i, P); 112193323Sed return MadeChange; 113193323Sed} 114193323Sed 115193323Sed/// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge 116193323Sed/// and return true, otherwise return false. This method requires that there be 117193323Sed/// an edge between the two blocks. If P is specified, it updates the analyses 118193323Sed/// described above. 119198090Srdivackyinline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 120198090Srdivacky Pass *P = 0, 121226633Sdim bool MergeIdenticalEdges = false, 122226633Sdim bool DontDeleteUselessPHIs = false) { 123193323Sed TerminatorInst *TI = Src->getTerminator(); 124193323Sed unsigned i = 0; 125193323Sed while (1) { 126193323Sed assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 127193323Sed if (TI->getSuccessor(i) == Dst) 128226633Sdim return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges, 129226633Sdim DontDeleteUselessPHIs); 130193323Sed ++i; 131193323Sed } 132193323Sed} 133193323Sed 134226633Sdim/// SplitEdge - Split the edge connecting specified block. Pass P must 135226633Sdim/// not be NULL. 136193323SedBasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P); 137193323Sed 138193323Sed/// SplitBlock - Split the specified block at the specified instruction - every 139193323Sed/// thing before SplitPt stays in Old and everything starting with SplitPt moves 140193323Sed/// to a new block. The two blocks are joined by an unconditional branch and 141193323Sed/// the loop info is updated. 142193323Sed/// 143193323SedBasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P); 144226633Sdim 145193323Sed/// SplitBlockPredecessors - This method transforms BB by introducing a new 146193323Sed/// basic block into the function, and moving some of the predecessors of BB to 147193323Sed/// be predecessors of the new block. The new predecessors are indicated by the 148193323Sed/// Preds array, which has NumPreds elements in it. The new block is given a 149193323Sed/// suffix of 'Suffix'. This function returns the new block. 150193323Sed/// 151198090Srdivacky/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 152198090Srdivacky/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. 153198090Srdivacky/// In particular, it does not preserve LoopSimplify (because it's 154198090Srdivacky/// complicated to handle the case where one of the edges being split 155198090Srdivacky/// is an exit of a loop with other exits). 156198090Srdivacky/// 157234353SdimBasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock*> Preds, 158234353Sdim const char *Suffix, Pass *P = 0); 159218893Sdim 160226633Sdim/// SplitLandingPadPredecessors - This method transforms the landing pad, 161226633Sdim/// OrigBB, by introducing two new basic blocks into the function. One of those 162226633Sdim/// new basic blocks gets the predecessors listed in Preds. The other basic 163226633Sdim/// block gets the remaining predecessors of OrigBB. The landingpad instruction 164226633Sdim/// OrigBB is clone into both of the new basic blocks. The new blocks are given 165226633Sdim/// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector. 166226633Sdim/// 167226633Sdim/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 168226633Sdim/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular, 169226633Sdim/// it does not preserve LoopSimplify (because it's complicated to handle the 170226633Sdim/// case where one of the edges being split is an exit of a loop with other 171226633Sdim/// exits). 172226633Sdim/// 173226633Sdimvoid SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds, 174226633Sdim const char *Suffix, const char *Suffix2, 175226633Sdim Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs); 176226633Sdim 177218893Sdim/// FoldReturnIntoUncondBranch - This method duplicates the specified return 178218893Sdim/// instruction into a predecessor which ends in an unconditional branch. If 179218893Sdim/// the return instruction returns a value defined by a PHI, propagate the 180218893Sdim/// right value into the return. It returns the new return instruction in the 181218893Sdim/// predecessor. 182218893SdimReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 183218893Sdim BasicBlock *Pred); 184218893Sdim 185243830Sdim/// SplitBlockAndInsertIfThen - Split the containing block at the 186243830Sdim/// specified instruction - everything before and including Cmp stays 187243830Sdim/// in the old basic block, and everything after Cmp is moved to a 188243830Sdim/// new block. The two blocks are connected by a conditional branch 189243830Sdim/// (with value of Cmp being the condition). 190243830Sdim/// Before: 191243830Sdim/// Head 192243830Sdim/// Cmp 193243830Sdim/// Tail 194243830Sdim/// After: 195243830Sdim/// Head 196243830Sdim/// Cmp 197243830Sdim/// if (Cmp) 198243830Sdim/// ThenBlock 199243830Sdim/// Tail 200243830Sdim/// 201243830Sdim/// If Unreachable is true, then ThenBlock ends with 202243830Sdim/// UnreachableInst, otherwise it branches to Tail. 203243830Sdim/// Returns the NewBasicBlock's terminator. 204243830Sdim 205243830SdimTerminatorInst *SplitBlockAndInsertIfThen(Instruction *Cmp, 206243830Sdim bool Unreachable, MDNode *BranchWeights = 0); 207243830Sdim 208261991Sdim/// 209261991Sdim/// GetIfCondition - Check whether BB is the merge point of a if-region. 210261991Sdim/// If so, return the boolean condition that determines which entry into 211261991Sdim/// BB will be taken. Also, return by references the block that will be 212261991Sdim/// entered from if the condition is true, and the block that will be 213261991Sdim/// entered if the condition is false. 214261991Sdim 215261991SdimValue *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 216261991Sdim BasicBlock *&IfFalse); 217193323Sed} // End llvm namespace 218193323Sed 219193323Sed#endif 220