1 //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This family of functions perform manipulations on basic blocks, and 11 // instructions contained within basic blocks. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 16 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 17 18 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock 19 20 #include "llvm/IR/BasicBlock.h" 21 #include "llvm/IR/CFG.h" 22 23 namespace llvm { 24 25 class AliasAnalysis; 26 class MemoryDependenceAnalysis; 27 class DominatorTree; 28 class LoopInfo; 29 class Instruction; 30 class MDNode; 31 class ReturnInst; 32 class TargetLibraryInfo; 33 class TerminatorInst; 34 35 /// DeleteDeadBlock - Delete the specified block, which must have no 36 /// predecessors. 37 void DeleteDeadBlock(BasicBlock *BB); 38 39 /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are 40 /// any single-entry PHI nodes in it, fold them away. This handles the case 41 /// when all entries to the PHI nodes in a block are guaranteed equal, such as 42 /// when the block has exactly one predecessor. 43 void FoldSingleEntryPHINodes(BasicBlock *BB, AliasAnalysis *AA = nullptr, 44 MemoryDependenceAnalysis *MemDep = nullptr); 45 46 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it 47 /// is dead. Also recursively delete any operands that become dead as 48 /// a result. This includes tracing the def-use list from the PHI to see if 49 /// it is ultimately unused or if it reaches an unused cycle. Return true 50 /// if any PHIs were deleted. 51 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr); 52 53 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, 54 /// if possible. The return value indicates success or failure. 55 bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT = nullptr, 56 LoopInfo *LI = nullptr, 57 AliasAnalysis *AA = nullptr, 58 MemoryDependenceAnalysis *MemDep = nullptr); 59 60 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) 61 // with a value, then remove and delete the original instruction. 62 // 63 void ReplaceInstWithValue(BasicBlock::InstListType &BIL, 64 BasicBlock::iterator &BI, Value *V); 65 66 // ReplaceInstWithInst - Replace the instruction specified by BI with the 67 // instruction specified by I. The original instruction is deleted and BI is 68 // updated to point to the new instruction. 69 // 70 void ReplaceInstWithInst(BasicBlock::InstListType &BIL, 71 BasicBlock::iterator &BI, Instruction *I); 72 73 // ReplaceInstWithInst - Replace the instruction specified by From with the 74 // instruction specified by To. 75 // 76 void ReplaceInstWithInst(Instruction *From, Instruction *To); 77 78 /// \brief Option class for critical edge splitting. 79 /// 80 /// This provides a builder interface for overriding the default options used 81 /// during critical edge splitting. 82 struct CriticalEdgeSplittingOptions { 83 AliasAnalysis *AA; 84 DominatorTree *DT; 85 LoopInfo *LI; 86 bool MergeIdenticalEdges; 87 bool DontDeleteUselessPHIs; 88 bool PreserveLCSSA; 89 CriticalEdgeSplittingOptionsCriticalEdgeSplittingOptions90 CriticalEdgeSplittingOptions() 91 : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false), 92 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {} 93 94 /// \brief Basic case of setting up all the analysis. 95 CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr, 96 LoopInfo *LI = nullptr) AACriticalEdgeSplittingOptions97 : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false), 98 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {} 99 100 /// \brief A common pattern is to preserve the dominator tree and loop 101 /// info but not care about AA. CriticalEdgeSplittingOptionsCriticalEdgeSplittingOptions102 CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI) 103 : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false), 104 DontDeleteUselessPHIs(false), PreserveLCSSA(false) {} 105 setMergeIdenticalEdgesCriticalEdgeSplittingOptions106 CriticalEdgeSplittingOptions &setMergeIdenticalEdges() { 107 MergeIdenticalEdges = true; 108 return *this; 109 } 110 setDontDeleteUselessPHIsCriticalEdgeSplittingOptions111 CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() { 112 DontDeleteUselessPHIs = true; 113 return *this; 114 } 115 setPreserveLCSSACriticalEdgeSplittingOptions116 CriticalEdgeSplittingOptions &setPreserveLCSSA() { 117 PreserveLCSSA = true; 118 return *this; 119 } 120 }; 121 122 /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to 123 /// split the critical edge. This will update the analyses passed in through 124 /// the option struct. This returns the new block if the edge was split, null 125 /// otherwise. 126 /// 127 /// If MergeIdenticalEdges in the options struct is true (not the default), 128 /// *all* edges from TI to the specified successor will be merged into the same 129 /// critical edge block. This is most commonly interesting with switch 130 /// instructions, which may have many edges to any one destination. This 131 /// ensures that all edges to that dest go to one block instead of each going 132 /// to a different block, but isn't the standard definition of a "critical 133 /// edge". 134 /// 135 /// It is invalid to call this function on a critical edge that starts at an 136 /// IndirectBrInst. Splitting these edges will almost always create an invalid 137 /// program because the address of the new block won't be the one that is jumped 138 /// to. 139 /// 140 BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, 141 const CriticalEdgeSplittingOptions &Options = 142 CriticalEdgeSplittingOptions()); 143 144 inline BasicBlock * 145 SplitCriticalEdge(BasicBlock *BB, succ_iterator SI, 146 const CriticalEdgeSplittingOptions &Options = 147 CriticalEdgeSplittingOptions()) { 148 return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(), 149 Options); 150 } 151 152 /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return 153 /// false. Otherwise, split all edges between the two blocks and return true. 154 /// This updates all of the same analyses as the other SplitCriticalEdge 155 /// function. If P is specified, it updates the analyses 156 /// described above. 157 inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI, 158 const CriticalEdgeSplittingOptions &Options = 159 CriticalEdgeSplittingOptions()) { 160 bool MadeChange = false; 161 TerminatorInst *TI = (*PI)->getTerminator(); 162 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 163 if (TI->getSuccessor(i) == Succ) 164 MadeChange |= !!SplitCriticalEdge(TI, i, Options); 165 return MadeChange; 166 } 167 168 /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge 169 /// and return true, otherwise return false. This method requires that there be 170 /// an edge between the two blocks. It updates the analyses 171 /// passed in the options struct 172 inline BasicBlock * 173 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 174 const CriticalEdgeSplittingOptions &Options = 175 CriticalEdgeSplittingOptions()) { 176 TerminatorInst *TI = Src->getTerminator(); 177 unsigned i = 0; 178 while (1) { 179 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 180 if (TI->getSuccessor(i) == Dst) 181 return SplitCriticalEdge(TI, i, Options); 182 ++i; 183 } 184 } 185 186 // SplitAllCriticalEdges - Loop over all of the edges in the CFG, 187 // breaking critical edges as they are found. 188 // Returns the number of broken edges. 189 unsigned SplitAllCriticalEdges(Function &F, 190 const CriticalEdgeSplittingOptions &Options = 191 CriticalEdgeSplittingOptions()); 192 193 /// SplitEdge - Split the edge connecting specified block. 194 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, 195 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr); 196 197 /// SplitBlock - Split the specified block at the specified instruction - every 198 /// thing before SplitPt stays in Old and everything starting with SplitPt moves 199 /// to a new block. The two blocks are joined by an unconditional branch and 200 /// the loop info is updated. 201 /// 202 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, 203 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr); 204 205 /// SplitBlockPredecessors - This method introduces at least one new basic block 206 /// into the function and moves some of the predecessors of BB to be 207 /// predecessors of the new block. The new predecessors are indicated by the 208 /// Preds array. The new block is given a suffix of 'Suffix'. Returns new basic 209 /// block to which predecessors from Preds are now pointing. 210 /// 211 /// If BB is a landingpad block then additional basicblock might be introduced. 212 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more 213 /// details on this case. 214 /// 215 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 216 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. 217 /// In particular, it does not preserve LoopSimplify (because it's 218 /// complicated to handle the case where one of the edges being split 219 /// is an exit of a loop with other exits). 220 /// 221 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 222 const char *Suffix, 223 AliasAnalysis *AA = nullptr, 224 DominatorTree *DT = nullptr, 225 LoopInfo *LI = nullptr, 226 bool PreserveLCSSA = false); 227 228 /// SplitLandingPadPredecessors - This method transforms the landing pad, 229 /// OrigBB, by introducing two new basic blocks into the function. One of those 230 /// new basic blocks gets the predecessors listed in Preds. The other basic 231 /// block gets the remaining predecessors of OrigBB. The landingpad instruction 232 /// OrigBB is clone into both of the new basic blocks. The new blocks are given 233 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector. 234 /// 235 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, 236 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular, 237 /// it does not preserve LoopSimplify (because it's complicated to handle the 238 /// case where one of the edges being split is an exit of a loop with other 239 /// exits). 240 /// 241 void SplitLandingPadPredecessors(BasicBlock *OrigBB, 242 ArrayRef<BasicBlock *> Preds, 243 const char *Suffix, const char *Suffix2, 244 SmallVectorImpl<BasicBlock *> &NewBBs, 245 AliasAnalysis *AA = nullptr, 246 DominatorTree *DT = nullptr, 247 LoopInfo *LI = nullptr, 248 bool PreserveLCSSA = false); 249 250 /// FoldReturnIntoUncondBranch - This method duplicates the specified return 251 /// instruction into a predecessor which ends in an unconditional branch. If 252 /// the return instruction returns a value defined by a PHI, propagate the 253 /// right value into the return. It returns the new return instruction in the 254 /// predecessor. 255 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 256 BasicBlock *Pred); 257 258 /// SplitBlockAndInsertIfThen - Split the containing block at the 259 /// specified instruction - everything before and including SplitBefore stays 260 /// in the old basic block, and everything after SplitBefore is moved to a 261 /// new block. The two blocks are connected by a conditional branch 262 /// (with value of Cmp being the condition). 263 /// Before: 264 /// Head 265 /// SplitBefore 266 /// Tail 267 /// After: 268 /// Head 269 /// if (Cond) 270 /// ThenBlock 271 /// SplitBefore 272 /// Tail 273 /// 274 /// If Unreachable is true, then ThenBlock ends with 275 /// UnreachableInst, otherwise it branches to Tail. 276 /// Returns the NewBasicBlock's terminator. 277 /// 278 /// Updates DT if given. 279 TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, 280 bool Unreachable, 281 MDNode *BranchWeights = nullptr, 282 DominatorTree *DT = nullptr); 283 284 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, 285 /// but also creates the ElseBlock. 286 /// Before: 287 /// Head 288 /// SplitBefore 289 /// Tail 290 /// After: 291 /// Head 292 /// if (Cond) 293 /// ThenBlock 294 /// else 295 /// ElseBlock 296 /// SplitBefore 297 /// Tail 298 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 299 TerminatorInst **ThenTerm, 300 TerminatorInst **ElseTerm, 301 MDNode *BranchWeights = nullptr); 302 303 /// 304 /// GetIfCondition - Check whether BB is the merge point of a if-region. 305 /// If so, return the boolean condition that determines which entry into 306 /// BB will be taken. Also, return by references the block that will be 307 /// entered from if the condition is true, and the block that will be 308 /// entered if the condition is false. 309 Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 310 BasicBlock *&IfFalse); 311 } // End llvm namespace 312 313 #endif 314