BasicBlockUtils.h revision 309124
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" 21276479Sdim#include "llvm/IR/CFG.h" 22193323Sed 23193323Sednamespace llvm { 24193323Sed 25309124Sdimclass MemoryDependenceResults; 26276479Sdimclass DominatorTree; 27288943Sdimclass LoopInfo; 28193323Sedclass Instruction; 29243830Sdimclass MDNode; 30218893Sdimclass ReturnInst; 31243830Sdimclass TargetLibraryInfo; 32243830Sdimclass TerminatorInst; 33193323Sed 34309124Sdim/// Delete the specified block, which must have no predecessors. 35193323Sedvoid DeleteDeadBlock(BasicBlock *BB); 36226633Sdim 37309124Sdim/// We know that BB has one predecessor. If there are any single-entry PHI nodes 38309124Sdim/// in it, fold them away. This handles the case when all entries to the PHI 39309124Sdim/// nodes in a block are guaranteed equal, such as when the block has exactly 40309124Sdim/// one predecessor. 41296417Sdimvoid FoldSingleEntryPHINodes(BasicBlock *BB, 42309124Sdim MemoryDependenceResults *MemDep = nullptr); 43193323Sed 44309124Sdim/// Examine each PHI in the given block and delete it if it is dead. Also 45309124Sdim/// recursively delete any operands that become dead as a result. This includes 46309124Sdim/// tracing the def-use list from the PHI to see if it is ultimately unused or 47309124Sdim/// if it reaches an unused cycle. Return true if any PHIs were deleted. 48276479Sdimbool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr); 49193323Sed 50309124Sdim/// Attempts to merge a block into its predecessor, if possible. The return 51309124Sdim/// value indicates success or failure. 52288943Sdimbool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT = nullptr, 53288943Sdim LoopInfo *LI = nullptr, 54309124Sdim MemoryDependenceResults *MemDep = nullptr); 55193323Sed 56309124Sdim/// Replace all uses of an instruction (specified by BI) with a value, then 57309124Sdim/// remove and delete the original instruction. 58193323Sedvoid ReplaceInstWithValue(BasicBlock::InstListType &BIL, 59193323Sed BasicBlock::iterator &BI, Value *V); 60193323Sed 61309124Sdim/// Replace the instruction specified by BI with the instruction specified by I. 62309124Sdim/// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The 63309124Sdim/// original instruction is deleted and BI is updated to point to the new 64309124Sdim/// instruction. 65193323Sedvoid ReplaceInstWithInst(BasicBlock::InstListType &BIL, 66193323Sed BasicBlock::iterator &BI, Instruction *I); 67193323Sed 68309124Sdim/// Replace the instruction specified by From with the instruction specified by 69309124Sdim/// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. 70193323Sedvoid ReplaceInstWithInst(Instruction *From, Instruction *To); 71193323Sed 72309124Sdim/// Option class for critical edge splitting. 73288943Sdim/// 74288943Sdim/// This provides a builder interface for overriding the default options used 75288943Sdim/// during critical edge splitting. 76288943Sdimstruct CriticalEdgeSplittingOptions { 77288943Sdim DominatorTree *DT; 78288943Sdim LoopInfo *LI; 79288943Sdim bool MergeIdenticalEdges; 80288943Sdim bool DontDeleteUselessPHIs; 81288943Sdim bool PreserveLCSSA; 82288943Sdim 83296417Sdim CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr, 84288943Sdim LoopInfo *LI = nullptr) 85296417Sdim : DT(DT), LI(LI), MergeIdenticalEdges(false), 86288943Sdim DontDeleteUselessPHIs(false), PreserveLCSSA(false) {} 87288943Sdim 88288943Sdim CriticalEdgeSplittingOptions &setMergeIdenticalEdges() { 89288943Sdim MergeIdenticalEdges = true; 90288943Sdim return *this; 91288943Sdim } 92288943Sdim 93288943Sdim CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() { 94288943Sdim DontDeleteUselessPHIs = true; 95288943Sdim return *this; 96288943Sdim } 97288943Sdim 98288943Sdim CriticalEdgeSplittingOptions &setPreserveLCSSA() { 99288943Sdim PreserveLCSSA = true; 100288943Sdim return *this; 101288943Sdim } 102288943Sdim}; 103288943Sdim 104309124Sdim/// If this edge is a critical edge, insert a new node to split the critical 105309124Sdim/// edge. This will update the analyses passed in through the option struct. 106309124Sdim/// This returns the new block if the edge was split, null otherwise. 107193323Sed/// 108288943Sdim/// If MergeIdenticalEdges in the options struct is true (not the default), 109288943Sdim/// *all* edges from TI to the specified successor will be merged into the same 110288943Sdim/// critical edge block. This is most commonly interesting with switch 111288943Sdim/// instructions, which may have many edges to any one destination. This 112288943Sdim/// ensures that all edges to that dest go to one block instead of each going 113288943Sdim/// to a different block, but isn't the standard definition of a "critical 114288943Sdim/// edge". 115193323Sed/// 116198892Srdivacky/// It is invalid to call this function on a critical edge that starts at an 117198892Srdivacky/// IndirectBrInst. Splitting these edges will almost always create an invalid 118198892Srdivacky/// program because the address of the new block won't be the one that is jumped 119198892Srdivacky/// to. 120198892Srdivacky/// 121198090SrdivackyBasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, 122288943Sdim const CriticalEdgeSplittingOptions &Options = 123288943Sdim CriticalEdgeSplittingOptions()); 124193323Sed 125288943Sdiminline BasicBlock * 126288943SdimSplitCriticalEdge(BasicBlock *BB, succ_iterator SI, 127288943Sdim const CriticalEdgeSplittingOptions &Options = 128288943Sdim CriticalEdgeSplittingOptions()) { 129288943Sdim return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), 130288943Sdim Options); 131193323Sed} 132193323Sed 133309124Sdim/// If the edge from *PI to BB is not critical, return false. Otherwise, split 134309124Sdim/// all edges between the two blocks and return true. This updates all of the 135309124Sdim/// same analyses as the other SplitCriticalEdge function. If P is specified, it 136309124Sdim/// updates the analyses described above. 137276479Sdiminline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, 138288943Sdim const CriticalEdgeSplittingOptions &Options = 139288943Sdim CriticalEdgeSplittingOptions()) { 140193323Sed bool MadeChange = false; 141193323Sed TerminatorInst *TI = (*PI)->getTerminator(); 142193323Sed for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 143193323Sed if (TI->getSuccessor(i) == Succ) 144288943Sdim MadeChange |= !!SplitCriticalEdge(TI, i, Options); 145193323Sed return MadeChange; 146193323Sed} 147193323Sed 148309124Sdim/// If an edge from Src to Dst is critical, split the edge and return true, 149309124Sdim/// otherwise return false. This method requires that there be an edge between 150309124Sdim/// the two blocks. It updates the analyses passed in the options struct 151288943Sdiminline BasicBlock * 152288943SdimSplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 153288943Sdim const CriticalEdgeSplittingOptions &Options = 154288943Sdim CriticalEdgeSplittingOptions()) { 155193323Sed TerminatorInst *TI = Src->getTerminator(); 156193323Sed unsigned i = 0; 157193323Sed while (1) { 158193323Sed assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 159193323Sed if (TI->getSuccessor(i) == Dst) 160288943Sdim return SplitCriticalEdge(TI, i, Options); 161193323Sed ++i; 162193323Sed } 163193323Sed} 164193323Sed 165309124Sdim/// Loop over all of the edges in the CFG, breaking critical edges as they are 166309124Sdim/// found. Returns the number of broken edges. 167288943Sdimunsigned SplitAllCriticalEdges(Function &F, 168288943Sdim const CriticalEdgeSplittingOptions &Options = 169288943Sdim CriticalEdgeSplittingOptions()); 170280031Sdim 171309124Sdim/// Split the edge connecting specified block. 172288943SdimBasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, 173288943Sdim DominatorTree *DT = nullptr, LoopInfo *LI = nullptr); 174193323Sed 175309124Sdim/// Split the specified block at the specified instruction - everything before 176309124Sdim/// SplitPt stays in Old and everything starting with SplitPt moves to a new 177309124Sdim/// block. The two blocks are joined by an unconditional branch and the loop 178309124Sdim/// info is updated. 179288943SdimBasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, 180288943Sdim DominatorTree *DT = nullptr, LoopInfo *LI = nullptr); 181226633Sdim 182309124Sdim/// This method introduces at least one new basic block into the function and 183309124Sdim/// moves some of the predecessors of BB to be predecessors of the new block. 184309124Sdim/// The new predecessors are indicated by the Preds array. The new block is 185309124Sdim/// given a suffix of 'Suffix'. Returns new basic block to which predecessors 186309124Sdim/// from Preds are now pointing. 187193323Sed/// 188288943Sdim/// If BB is a landingpad block then additional basicblock might be introduced. 189288943Sdim/// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more 190288943Sdim/// details on this case. 191288943Sdim/// 192296417Sdim/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 193296417Sdim/// no other analyses. In particular, it does not preserve LoopSimplify 194296417Sdim/// (because it's complicated to handle the case where one of the edges being 195296417Sdim/// split is an exit of a loop with other exits). 196198090Srdivacky/// 197288943SdimBasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 198288943Sdim const char *Suffix, 199288943Sdim DominatorTree *DT = nullptr, 200288943Sdim LoopInfo *LI = nullptr, 201288943Sdim bool PreserveLCSSA = false); 202218893Sdim 203309124Sdim/// This method transforms the landing pad, OrigBB, by introducing two new basic 204309124Sdim/// blocks into the function. One of those new basic blocks gets the 205309124Sdim/// predecessors listed in Preds. The other basic block gets the remaining 206309124Sdim/// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both 207309124Sdim/// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and 208309124Sdim/// 'Suffix2', and are returned in the NewBBs vector. 209226633Sdim/// 210296417Sdim/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 211296417Sdim/// no other analyses. In particular, it does not preserve LoopSimplify 212296417Sdim/// (because it's complicated to handle the case where one of the edges being 213296417Sdim/// split is an exit of a loop with other exits). 214226633Sdim/// 215288943Sdimvoid SplitLandingPadPredecessors(BasicBlock *OrigBB, 216288943Sdim ArrayRef<BasicBlock *> Preds, 217226633Sdim const char *Suffix, const char *Suffix2, 218288943Sdim SmallVectorImpl<BasicBlock *> &NewBBs, 219288943Sdim DominatorTree *DT = nullptr, 220288943Sdim LoopInfo *LI = nullptr, 221288943Sdim bool PreserveLCSSA = false); 222226633Sdim 223309124Sdim/// This method duplicates the specified return instruction into a predecessor 224309124Sdim/// which ends in an unconditional branch. If the return instruction returns a 225309124Sdim/// value defined by a PHI, propagate the right value into the return. It 226309124Sdim/// returns the new return instruction in the predecessor. 227218893SdimReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 228218893Sdim BasicBlock *Pred); 229218893Sdim 230309124Sdim/// Split the containing block at the specified instruction - everything before 231309124Sdim/// and including SplitBefore stays in the old basic block, and everything after 232309124Sdim/// SplitBefore is moved to a new block. The two blocks are connected by a 233309124Sdim/// conditional branch (with value of Cmp being the condition). 234243830Sdim/// Before: 235243830Sdim/// Head 236276479Sdim/// SplitBefore 237243830Sdim/// Tail 238243830Sdim/// After: 239243830Sdim/// Head 240276479Sdim/// if (Cond) 241243830Sdim/// ThenBlock 242276479Sdim/// SplitBefore 243243830Sdim/// Tail 244243830Sdim/// 245243830Sdim/// If Unreachable is true, then ThenBlock ends with 246243830Sdim/// UnreachableInst, otherwise it branches to Tail. 247243830Sdim/// Returns the NewBasicBlock's terminator. 248276479Sdim/// 249309124Sdim/// Updates DT and LI if given. 250276479SdimTerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, 251276479Sdim bool Unreachable, 252276479Sdim MDNode *BranchWeights = nullptr, 253309124Sdim DominatorTree *DT = nullptr, 254309124Sdim LoopInfo *LI = nullptr); 255243830Sdim 256276479Sdim/// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, 257276479Sdim/// but also creates the ElseBlock. 258276479Sdim/// Before: 259276479Sdim/// Head 260276479Sdim/// SplitBefore 261276479Sdim/// Tail 262276479Sdim/// After: 263276479Sdim/// Head 264276479Sdim/// if (Cond) 265276479Sdim/// ThenBlock 266276479Sdim/// else 267276479Sdim/// ElseBlock 268276479Sdim/// SplitBefore 269276479Sdim/// Tail 270276479Sdimvoid SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 271276479Sdim TerminatorInst **ThenTerm, 272276479Sdim TerminatorInst **ElseTerm, 273276479Sdim MDNode *BranchWeights = nullptr); 274243830Sdim 275309124Sdim/// Check whether BB is the merge point of a if-region. 276261991Sdim/// If so, return the boolean condition that determines which entry into 277261991Sdim/// BB will be taken. Also, return by references the block that will be 278261991Sdim/// entered from if the condition is true, and the block that will be 279261991Sdim/// entered if the condition is false. 280309124Sdim/// 281309124Sdim/// This does no checking to see if the true/false blocks have large or unsavory 282309124Sdim/// instructions in them. 283261991SdimValue *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 284276479Sdim BasicBlock *&IfFalse); 285193323Sed} // End llvm namespace 286193323Sed 287193323Sed#endif 288