LLVM API Documentation
00001 //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This family of functions perform manipulations on basic blocks, and 00011 // instructions contained within basic blocks. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 00016 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 00017 00018 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock 00019 00020 #include "llvm/IR/BasicBlock.h" 00021 #include "llvm/Support/CFG.h" 00022 00023 namespace llvm { 00024 00025 class AliasAnalysis; 00026 class Instruction; 00027 class MDNode; 00028 class Pass; 00029 class ReturnInst; 00030 class TargetLibraryInfo; 00031 class TerminatorInst; 00032 00033 /// DeleteDeadBlock - Delete the specified block, which must have no 00034 /// predecessors. 00035 void DeleteDeadBlock(BasicBlock *BB); 00036 00037 00038 /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are 00039 /// any single-entry PHI nodes in it, fold them away. This handles the case 00040 /// when all entries to the PHI nodes in a block are guaranteed equal, such as 00041 /// when the block has exactly one predecessor. 00042 void FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P = 0); 00043 00044 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it 00045 /// is dead. Also recursively delete any operands that become dead as 00046 /// a result. This includes tracing the def-use list from the PHI to see if 00047 /// it is ultimately unused or if it reaches an unused cycle. Return true 00048 /// if any PHIs were deleted. 00049 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = 0); 00050 00051 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, 00052 /// if possible. The return value indicates success or failure. 00053 bool MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P = 0); 00054 00055 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) 00056 // with a value, then remove and delete the original instruction. 00057 // 00058 void ReplaceInstWithValue(BasicBlock::InstListType &BIL, 00059 BasicBlock::iterator &BI, Value *V); 00060 00061 // ReplaceInstWithInst - Replace the instruction specified by BI with the 00062 // instruction specified by I. The original instruction is deleted and BI is 00063 // updated to point to the new instruction. 00064 // 00065 void ReplaceInstWithInst(BasicBlock::InstListType &BIL, 00066 BasicBlock::iterator &BI, Instruction *I); 00067 00068 // ReplaceInstWithInst - Replace the instruction specified by From with the 00069 // instruction specified by To. 00070 // 00071 void ReplaceInstWithInst(Instruction *From, Instruction *To); 00072 00073 /// FindFunctionBackedges - Analyze the specified function to find all of the 00074 /// loop backedges in the function and return them. This is a relatively cheap 00075 /// (compared to computing dominators and loop info) analysis. 00076 /// 00077 /// The output is added to Result, as pairs of <from,to> edge info. 00078 void FindFunctionBackedges(const Function &F, 00079 SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result); 00080 00081 00082 /// GetSuccessorNumber - Search for the specified successor of basic block BB 00083 /// and return its position in the terminator instruction's list of 00084 /// successors. It is an error to call this with a block that is not a 00085 /// successor. 00086 unsigned GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ); 00087 00088 /// isCriticalEdge - Return true if the specified edge is a critical edge. 00089 /// Critical edges are edges from a block with multiple successors to a block 00090 /// with multiple predecessors. 00091 /// 00092 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, 00093 bool AllowIdenticalEdges = false); 00094 00095 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to 00096 /// split the critical edge. This will update DominatorTree and 00097 /// DominatorFrontier information if it is available, thus calling this pass 00098 /// will not invalidate either of them. This returns the new block if the edge 00099 /// was split, null otherwise. 00100 /// 00101 /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the 00102 /// specified successor will be merged into the same critical edge block. 00103 /// This is most commonly interesting with switch instructions, which may 00104 /// have many edges to any one destination. This ensures that all edges to that 00105 /// dest go to one block instead of each going to a different block, but isn't 00106 /// the standard definition of a "critical edge". 00107 /// 00108 /// It is invalid to call this function on a critical edge that starts at an 00109 /// IndirectBrInst. Splitting these edges will almost always create an invalid 00110 /// program because the address of the new block won't be the one that is jumped 00111 /// to. 00112 /// 00113 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, 00114 Pass *P = 0, bool MergeIdenticalEdges = false, 00115 bool DontDeleteUselessPHIs = false, 00116 bool SplitLandingPads = false); 00117 00118 inline BasicBlock *SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, 00119 Pass *P = 0) { 00120 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), P); 00121 } 00122 00123 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return 00124 /// false. Otherwise, split all edges between the two blocks and return true. 00125 /// This updates all of the same analyses as the other SplitCriticalEdge 00126 /// function. If P is specified, it updates the analyses 00127 /// described above. 00128 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, Pass *P = 0) { 00129 bool MadeChange = false; 00130 TerminatorInst *TI = (*PI)->getTerminator(); 00131 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 00132 if (TI->getSuccessor(i) == Succ) 00133 MadeChange |= !!SplitCriticalEdge(TI, i, P); 00134 return MadeChange; 00135 } 00136 00137 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge 00138 /// and return true, otherwise return false. This method requires that there be 00139 /// an edge between the two blocks. If P is specified, it updates the analyses 00140 /// described above. 00141 inline BasicBlock *SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 00142 Pass *P = 0, 00143 bool MergeIdenticalEdges = false, 00144 bool DontDeleteUselessPHIs = false) { 00145 TerminatorInst *TI = Src->getTerminator(); 00146 unsigned i = 0; 00147 while (1) { 00148 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 00149 if (TI->getSuccessor(i) == Dst) 00150 return SplitCriticalEdge(TI, i, P, MergeIdenticalEdges, 00151 DontDeleteUselessPHIs); 00152 ++i; 00153 } 00154 } 00155 00156 /// SplitEdge - Split the edge connecting specified block. Pass P must 00157 /// not be NULL. 00158 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, Pass *P); 00159 00160 /// SplitBlock - Split the specified block at the specified instruction - every 00161 /// thing before SplitPt stays in Old and everything starting with SplitPt moves 00162 /// to a new block. The two blocks are joined by an unconditional branch and 00163 /// the loop info is updated. 00164 /// 00165 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P); 00166 00167 /// SplitBlockPredecessors - This method transforms BB by introducing a new 00168 /// basic block into the function, and moving some of the predecessors of BB to 00169 /// be predecessors of the new block. The new predecessors are indicated by the 00170 /// Preds array, which has NumPreds elements in it. The new block is given a 00171 /// suffix of 'Suffix'. This function returns the new block. 00172 /// 00173 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 00174 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. 00175 /// In particular, it does not preserve LoopSimplify (because it's 00176 /// complicated to handle the case where one of the edges being split 00177 /// is an exit of a loop with other exits). 00178 /// 00179 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock*> Preds, 00180 const char *Suffix, Pass *P = 0); 00181 00182 /// SplitLandingPadPredecessors - This method transforms the landing pad, 00183 /// OrigBB, by introducing two new basic blocks into the function. One of those 00184 /// new basic blocks gets the predecessors listed in Preds. The other basic 00185 /// block gets the remaining predecessors of OrigBB. The landingpad instruction 00186 /// OrigBB is clone into both of the new basic blocks. The new blocks are given 00187 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector. 00188 /// 00189 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 00190 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular, 00191 /// it does not preserve LoopSimplify (because it's complicated to handle the 00192 /// case where one of the edges being split is an exit of a loop with other 00193 /// exits). 00194 /// 00195 void SplitLandingPadPredecessors(BasicBlock *OrigBB,ArrayRef<BasicBlock*> Preds, 00196 const char *Suffix, const char *Suffix2, 00197 Pass *P, SmallVectorImpl<BasicBlock*> &NewBBs); 00198 00199 /// FoldReturnIntoUncondBranch - This method duplicates the specified return 00200 /// instruction into a predecessor which ends in an unconditional branch. If 00201 /// the return instruction returns a value defined by a PHI, propagate the 00202 /// right value into the return. It returns the new return instruction in the 00203 /// predecessor. 00204 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 00205 BasicBlock *Pred); 00206 00207 /// SplitBlockAndInsertIfThen - Split the containing block at the 00208 /// specified instruction - everything before and including Cmp stays 00209 /// in the old basic block, and everything after Cmp is moved to a 00210 /// new block. The two blocks are connected by a conditional branch 00211 /// (with value of Cmp being the condition). 00212 /// Before: 00213 /// Head 00214 /// Cmp 00215 /// Tail 00216 /// After: 00217 /// Head 00218 /// Cmp 00219 /// if (Cmp) 00220 /// ThenBlock 00221 /// Tail 00222 /// 00223 /// If Unreachable is true, then ThenBlock ends with 00224 /// UnreachableInst, otherwise it branches to Tail. 00225 /// Returns the NewBasicBlock's terminator. 00226 00227 TerminatorInst *SplitBlockAndInsertIfThen(Instruction *Cmp, 00228 bool Unreachable, MDNode *BranchWeights = 0); 00229 00230 } // End llvm namespace 00231 00232 #endif