BasicBlockUtils.h revision 321369
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 20314564Sdim#include "llvm/ADT/ArrayRef.h" 21249423Sdim#include "llvm/IR/BasicBlock.h" 22276479Sdim#include "llvm/IR/CFG.h" 23314564Sdim#include "llvm/IR/InstrTypes.h" 24314564Sdim#include <cassert> 25193323Sed 26193323Sednamespace llvm { 27193323Sed 28309124Sdimclass MemoryDependenceResults; 29276479Sdimclass DominatorTree; 30288943Sdimclass LoopInfo; 31193323Sedclass Instruction; 32243830Sdimclass MDNode; 33218893Sdimclass ReturnInst; 34243830Sdimclass TargetLibraryInfo; 35193323Sed 36309124Sdim/// Delete the specified block, which must have no predecessors. 37193323Sedvoid DeleteDeadBlock(BasicBlock *BB); 38226633Sdim 39309124Sdim/// We know that BB has one predecessor. If there are any single-entry PHI nodes 40309124Sdim/// in it, fold them away. This handles the case when all entries to the PHI 41309124Sdim/// nodes in a block are guaranteed equal, such as when the block has exactly 42309124Sdim/// one predecessor. 43296417Sdimvoid FoldSingleEntryPHINodes(BasicBlock *BB, 44309124Sdim MemoryDependenceResults *MemDep = nullptr); 45193323Sed 46309124Sdim/// Examine each PHI in the given block and delete it if it is dead. Also 47309124Sdim/// recursively delete any operands that become dead as a result. This includes 48309124Sdim/// tracing the def-use list from the PHI to see if it is ultimately unused or 49309124Sdim/// if it reaches an unused cycle. Return true if any PHIs were deleted. 50276479Sdimbool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr); 51193323Sed 52309124Sdim/// Attempts to merge a block into its predecessor, if possible. The return 53309124Sdim/// value indicates success or failure. 54288943Sdimbool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT = nullptr, 55288943Sdim LoopInfo *LI = nullptr, 56309124Sdim MemoryDependenceResults *MemDep = nullptr); 57193323Sed 58309124Sdim/// Replace all uses of an instruction (specified by BI) with a value, then 59309124Sdim/// remove and delete the original instruction. 60193323Sedvoid ReplaceInstWithValue(BasicBlock::InstListType &BIL, 61193323Sed BasicBlock::iterator &BI, Value *V); 62193323Sed 63309124Sdim/// Replace the instruction specified by BI with the instruction specified by I. 64309124Sdim/// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The 65309124Sdim/// original instruction is deleted and BI is updated to point to the new 66309124Sdim/// instruction. 67193323Sedvoid ReplaceInstWithInst(BasicBlock::InstListType &BIL, 68193323Sed BasicBlock::iterator &BI, Instruction *I); 69193323Sed 70309124Sdim/// Replace the instruction specified by From with the instruction specified by 71309124Sdim/// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. 72193323Sedvoid ReplaceInstWithInst(Instruction *From, Instruction *To); 73193323Sed 74309124Sdim/// Option class for critical edge splitting. 75288943Sdim/// 76288943Sdim/// This provides a builder interface for overriding the default options used 77288943Sdim/// during critical edge splitting. 78288943Sdimstruct CriticalEdgeSplittingOptions { 79288943Sdim DominatorTree *DT; 80288943Sdim LoopInfo *LI; 81321369Sdim bool MergeIdenticalEdges = false; 82321369Sdim bool DontDeleteUselessPHIs = false; 83321369Sdim bool PreserveLCSSA = false; 84288943Sdim 85296417Sdim CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr, 86288943Sdim LoopInfo *LI = nullptr) 87321369Sdim : DT(DT), LI(LI) {} 88288943Sdim 89288943Sdim CriticalEdgeSplittingOptions &setMergeIdenticalEdges() { 90288943Sdim MergeIdenticalEdges = true; 91288943Sdim return *this; 92288943Sdim } 93288943Sdim 94288943Sdim CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() { 95288943Sdim DontDeleteUselessPHIs = true; 96288943Sdim return *this; 97288943Sdim } 98288943Sdim 99288943Sdim CriticalEdgeSplittingOptions &setPreserveLCSSA() { 100288943Sdim PreserveLCSSA = true; 101288943Sdim return *this; 102288943Sdim } 103288943Sdim}; 104288943Sdim 105309124Sdim/// If this edge is a critical edge, insert a new node to split the critical 106309124Sdim/// edge. This will update the analyses passed in through the option struct. 107309124Sdim/// This returns the new block if the edge was split, null otherwise. 108193323Sed/// 109288943Sdim/// If MergeIdenticalEdges in the options struct is true (not the default), 110288943Sdim/// *all* edges from TI to the specified successor will be merged into the same 111288943Sdim/// critical edge block. This is most commonly interesting with switch 112288943Sdim/// instructions, which may have many edges to any one destination. This 113288943Sdim/// ensures that all edges to that dest go to one block instead of each going 114288943Sdim/// to a different block, but isn't the standard definition of a "critical 115288943Sdim/// edge". 116193323Sed/// 117198892Srdivacky/// It is invalid to call this function on a critical edge that starts at an 118198892Srdivacky/// IndirectBrInst. Splitting these edges will almost always create an invalid 119198892Srdivacky/// program because the address of the new block won't be the one that is jumped 120198892Srdivacky/// to. 121198892Srdivacky/// 122198090SrdivackyBasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, 123288943Sdim const CriticalEdgeSplittingOptions &Options = 124288943Sdim CriticalEdgeSplittingOptions()); 125193323Sed 126288943Sdiminline BasicBlock * 127288943SdimSplitCriticalEdge(BasicBlock *BB, succ_iterator SI, 128288943Sdim const CriticalEdgeSplittingOptions &Options = 129288943Sdim CriticalEdgeSplittingOptions()) { 130288943Sdim return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), 131288943Sdim Options); 132193323Sed} 133193323Sed 134309124Sdim/// If the edge from *PI to BB is not critical, return false. Otherwise, split 135309124Sdim/// all edges between the two blocks and return true. This updates all of the 136309124Sdim/// same analyses as the other SplitCriticalEdge function. If P is specified, it 137309124Sdim/// updates the analyses described above. 138276479Sdiminline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, 139288943Sdim const CriticalEdgeSplittingOptions &Options = 140288943Sdim CriticalEdgeSplittingOptions()) { 141193323Sed bool MadeChange = false; 142193323Sed TerminatorInst *TI = (*PI)->getTerminator(); 143193323Sed for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 144193323Sed if (TI->getSuccessor(i) == Succ) 145288943Sdim MadeChange |= !!SplitCriticalEdge(TI, i, Options); 146193323Sed return MadeChange; 147193323Sed} 148193323Sed 149309124Sdim/// If an edge from Src to Dst is critical, split the edge and return true, 150309124Sdim/// otherwise return false. This method requires that there be an edge between 151309124Sdim/// the two blocks. It updates the analyses passed in the options struct 152288943Sdiminline BasicBlock * 153288943SdimSplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 154288943Sdim const CriticalEdgeSplittingOptions &Options = 155288943Sdim CriticalEdgeSplittingOptions()) { 156193323Sed TerminatorInst *TI = Src->getTerminator(); 157193323Sed unsigned i = 0; 158314564Sdim while (true) { 159193323Sed assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 160193323Sed if (TI->getSuccessor(i) == Dst) 161288943Sdim return SplitCriticalEdge(TI, i, Options); 162193323Sed ++i; 163193323Sed } 164193323Sed} 165193323Sed 166309124Sdim/// Loop over all of the edges in the CFG, breaking critical edges as they are 167309124Sdim/// found. Returns the number of broken edges. 168288943Sdimunsigned SplitAllCriticalEdges(Function &F, 169288943Sdim const CriticalEdgeSplittingOptions &Options = 170288943Sdim CriticalEdgeSplittingOptions()); 171280031Sdim 172309124Sdim/// Split the edge connecting specified block. 173288943SdimBasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, 174288943Sdim DominatorTree *DT = nullptr, LoopInfo *LI = nullptr); 175193323Sed 176309124Sdim/// Split the specified block at the specified instruction - everything before 177309124Sdim/// SplitPt stays in Old and everything starting with SplitPt moves to a new 178309124Sdim/// block. The two blocks are joined by an unconditional branch and the loop 179309124Sdim/// info is updated. 180288943SdimBasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, 181288943Sdim DominatorTree *DT = nullptr, LoopInfo *LI = nullptr); 182226633Sdim 183309124Sdim/// This method introduces at least one new basic block into the function and 184309124Sdim/// moves some of the predecessors of BB to be predecessors of the new block. 185309124Sdim/// The new predecessors are indicated by the Preds array. The new block is 186309124Sdim/// given a suffix of 'Suffix'. Returns new basic block to which predecessors 187309124Sdim/// from Preds are now pointing. 188193323Sed/// 189288943Sdim/// If BB is a landingpad block then additional basicblock might be introduced. 190288943Sdim/// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more 191288943Sdim/// details on this case. 192288943Sdim/// 193296417Sdim/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 194296417Sdim/// no other analyses. In particular, it does not preserve LoopSimplify 195296417Sdim/// (because it's complicated to handle the case where one of the edges being 196296417Sdim/// split is an exit of a loop with other exits). 197198090Srdivacky/// 198288943SdimBasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 199288943Sdim const char *Suffix, 200288943Sdim DominatorTree *DT = nullptr, 201288943Sdim LoopInfo *LI = nullptr, 202288943Sdim bool PreserveLCSSA = false); 203218893Sdim 204309124Sdim/// This method transforms the landing pad, OrigBB, by introducing two new basic 205309124Sdim/// blocks into the function. One of those new basic blocks gets the 206309124Sdim/// predecessors listed in Preds. The other basic block gets the remaining 207309124Sdim/// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both 208309124Sdim/// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and 209309124Sdim/// 'Suffix2', and are returned in the NewBBs vector. 210226633Sdim/// 211296417Sdim/// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 212296417Sdim/// no other analyses. In particular, it does not preserve LoopSimplify 213296417Sdim/// (because it's complicated to handle the case where one of the edges being 214296417Sdim/// split is an exit of a loop with other exits). 215226633Sdim/// 216288943Sdimvoid SplitLandingPadPredecessors(BasicBlock *OrigBB, 217288943Sdim ArrayRef<BasicBlock *> Preds, 218226633Sdim const char *Suffix, const char *Suffix2, 219288943Sdim SmallVectorImpl<BasicBlock *> &NewBBs, 220288943Sdim DominatorTree *DT = nullptr, 221288943Sdim LoopInfo *LI = nullptr, 222288943Sdim bool PreserveLCSSA = false); 223226633Sdim 224309124Sdim/// This method duplicates the specified return instruction into a predecessor 225309124Sdim/// which ends in an unconditional branch. If the return instruction returns a 226309124Sdim/// value defined by a PHI, propagate the right value into the return. It 227309124Sdim/// returns the new return instruction in the predecessor. 228218893SdimReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 229218893Sdim BasicBlock *Pred); 230218893Sdim 231309124Sdim/// Split the containing block at the specified instruction - everything before 232314564Sdim/// SplitBefore stays in the old basic block, and the rest of the instructions 233314564Sdim/// in the BB are moved to a new block. The two blocks are connected by a 234309124Sdim/// conditional branch (with value of Cmp being the condition). 235243830Sdim/// Before: 236243830Sdim/// Head 237276479Sdim/// SplitBefore 238243830Sdim/// Tail 239243830Sdim/// After: 240243830Sdim/// Head 241276479Sdim/// if (Cond) 242243830Sdim/// ThenBlock 243276479Sdim/// SplitBefore 244243830Sdim/// Tail 245243830Sdim/// 246243830Sdim/// If Unreachable is true, then ThenBlock ends with 247243830Sdim/// UnreachableInst, otherwise it branches to Tail. 248243830Sdim/// Returns the NewBasicBlock's terminator. 249276479Sdim/// 250309124Sdim/// Updates DT and LI if given. 251276479SdimTerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, 252276479Sdim bool Unreachable, 253276479Sdim MDNode *BranchWeights = nullptr, 254309124Sdim DominatorTree *DT = nullptr, 255309124Sdim LoopInfo *LI = nullptr); 256243830Sdim 257276479Sdim/// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, 258276479Sdim/// but also creates the ElseBlock. 259276479Sdim/// Before: 260276479Sdim/// Head 261276479Sdim/// SplitBefore 262276479Sdim/// Tail 263276479Sdim/// After: 264276479Sdim/// Head 265276479Sdim/// if (Cond) 266276479Sdim/// ThenBlock 267276479Sdim/// else 268276479Sdim/// ElseBlock 269276479Sdim/// SplitBefore 270276479Sdim/// Tail 271276479Sdimvoid SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 272276479Sdim TerminatorInst **ThenTerm, 273276479Sdim TerminatorInst **ElseTerm, 274276479Sdim MDNode *BranchWeights = nullptr); 275243830Sdim 276309124Sdim/// Check whether BB is the merge point of a if-region. 277261991Sdim/// If so, return the boolean condition that determines which entry into 278261991Sdim/// BB will be taken. Also, return by references the block that will be 279261991Sdim/// entered from if the condition is true, and the block that will be 280261991Sdim/// entered if the condition is false. 281309124Sdim/// 282309124Sdim/// This does no checking to see if the true/false blocks have large or unsavory 283309124Sdim/// instructions in them. 284261991SdimValue *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 285276479Sdim BasicBlock *&IfFalse); 286193323Sed 287314564Sdim} // end namespace llvm 288314564Sdim 289314564Sdim#endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 290