|
|
|||
File indexing completed on 2026-05-10 08:44:42
0001 //===- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils -----*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This family of functions perform manipulations on basic blocks, and 0010 // instructions contained within basic blocks. 0011 // 0012 //===----------------------------------------------------------------------===// 0013 0014 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 0015 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 0016 0017 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock 0018 0019 #include "llvm/ADT/ArrayRef.h" 0020 #include "llvm/ADT/SetVector.h" 0021 #include "llvm/IR/BasicBlock.h" 0022 #include "llvm/IR/Dominators.h" 0023 #include <cassert> 0024 0025 namespace llvm { 0026 class BranchInst; 0027 class LandingPadInst; 0028 class Loop; 0029 class PHINode; 0030 template <typename PtrType> class SmallPtrSetImpl; 0031 class BlockFrequencyInfo; 0032 class BranchProbabilityInfo; 0033 class DomTreeUpdater; 0034 class Function; 0035 class IRBuilderBase; 0036 class LoopInfo; 0037 class MDNode; 0038 class MemoryDependenceResults; 0039 class MemorySSAUpdater; 0040 class PostDominatorTree; 0041 class ReturnInst; 0042 class TargetLibraryInfo; 0043 class Value; 0044 0045 /// Replace contents of every block in \p BBs with single unreachable 0046 /// instruction. If \p Updates is specified, collect all necessary DT updates 0047 /// into this vector. If \p KeepOneInputPHIs is true, one-input Phis in 0048 /// successors of blocks being deleted will be preserved. 0049 void detachDeadBlocks(ArrayRef <BasicBlock *> BBs, 0050 SmallVectorImpl<DominatorTree::UpdateType> *Updates, 0051 bool KeepOneInputPHIs = false); 0052 0053 /// Delete the specified block, which must have no predecessors. 0054 void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU = nullptr, 0055 bool KeepOneInputPHIs = false); 0056 0057 /// Delete the specified blocks from \p BB. The set of deleted blocks must have 0058 /// no predecessors that are not being deleted themselves. \p BBs must have no 0059 /// duplicating blocks. If there are loops among this set of blocks, all 0060 /// relevant loop info updates should be done before this function is called. 0061 /// If \p KeepOneInputPHIs is true, one-input Phis in successors of blocks 0062 /// being deleted will be preserved. 0063 void DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, 0064 DomTreeUpdater *DTU = nullptr, 0065 bool KeepOneInputPHIs = false); 0066 0067 /// Delete all basic blocks from \p F that are not reachable from its entry 0068 /// node. If \p KeepOneInputPHIs is true, one-input Phis in successors of 0069 /// blocks being deleted will be preserved. 0070 bool EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU = nullptr, 0071 bool KeepOneInputPHIs = false); 0072 0073 /// We know that BB has one predecessor. If there are any single-entry PHI nodes 0074 /// in it, fold them away. This handles the case when all entries to the PHI 0075 /// nodes in a block are guaranteed equal, such as when the block has exactly 0076 /// one predecessor. 0077 bool FoldSingleEntryPHINodes(BasicBlock *BB, 0078 MemoryDependenceResults *MemDep = nullptr); 0079 0080 /// Examine each PHI in the given block and delete it if it is dead. Also 0081 /// recursively delete any operands that become dead as a result. This includes 0082 /// tracing the def-use list from the PHI to see if it is ultimately unused or 0083 /// if it reaches an unused cycle. Return true if any PHIs were deleted. 0084 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr, 0085 MemorySSAUpdater *MSSAU = nullptr); 0086 0087 /// Attempts to merge a block into its predecessor, if possible. The return 0088 /// value indicates success or failure. 0089 /// By default do not merge blocks if BB's predecessor has multiple successors. 0090 /// If PredecessorWithTwoSuccessors = true, the blocks can only be merged 0091 /// if BB's Pred has a branch to BB and to AnotherBB, and BB has a single 0092 /// successor Sing. In this case the branch will be updated with Sing instead of 0093 /// BB, and BB will still be merged into its predecessor and removed. 0094 /// If \p DT is not nullptr, update it directly; in that case, DTU must be 0095 /// nullptr. 0096 bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU = nullptr, 0097 LoopInfo *LI = nullptr, 0098 MemorySSAUpdater *MSSAU = nullptr, 0099 MemoryDependenceResults *MemDep = nullptr, 0100 bool PredecessorWithTwoSuccessors = false, 0101 DominatorTree *DT = nullptr); 0102 0103 /// Merge block(s) sucessors, if possible. Return true if at least two 0104 /// of the blocks were merged together. 0105 /// In order to merge, each block must be terminated by an unconditional 0106 /// branch. If L is provided, then the blocks merged into their predecessors 0107 /// must be in L. In addition, This utility calls on another utility: 0108 /// MergeBlockIntoPredecessor. Blocks are successfully merged when the call to 0109 /// MergeBlockIntoPredecessor returns true. 0110 bool MergeBlockSuccessorsIntoGivenBlocks( 0111 SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L = nullptr, 0112 DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr); 0113 0114 /// Try to remove redundant dbg.value instructions from given basic block. 0115 /// Returns true if at least one instruction was removed. Remove redundant 0116 /// pseudo ops when RemovePseudoOp is true. 0117 bool RemoveRedundantDbgInstrs(BasicBlock *BB); 0118 0119 /// Replace all uses of an instruction (specified by BI) with a value, then 0120 /// remove and delete the original instruction. 0121 void ReplaceInstWithValue(BasicBlock::iterator &BI, Value *V); 0122 0123 /// Replace the instruction specified by BI with the instruction specified by I. 0124 /// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The 0125 /// original instruction is deleted and BI is updated to point to the new 0126 /// instruction. 0127 void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI, 0128 Instruction *I); 0129 0130 /// Replace the instruction specified by From with the instruction specified by 0131 /// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. 0132 void ReplaceInstWithInst(Instruction *From, Instruction *To); 0133 0134 /// Check if we can prove that all paths starting from this block converge 0135 /// to a block that either has a @llvm.experimental.deoptimize call 0136 /// prior to its terminating return instruction or is terminated by unreachable. 0137 /// All blocks in the traversed sequence must have an unique successor, maybe 0138 /// except for the last one. 0139 bool IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB); 0140 0141 /// Option class for critical edge splitting. 0142 /// 0143 /// This provides a builder interface for overriding the default options used 0144 /// during critical edge splitting. 0145 struct CriticalEdgeSplittingOptions { 0146 DominatorTree *DT; 0147 PostDominatorTree *PDT; 0148 LoopInfo *LI; 0149 MemorySSAUpdater *MSSAU; 0150 bool MergeIdenticalEdges = false; 0151 bool KeepOneInputPHIs = false; 0152 bool PreserveLCSSA = false; 0153 bool IgnoreUnreachableDests = false; 0154 /// SplitCriticalEdge is guaranteed to preserve loop-simplify form if LI is 0155 /// provided. If it cannot be preserved, no splitting will take place. If it 0156 /// is not set, preserve loop-simplify form if possible. 0157 bool PreserveLoopSimplify = true; 0158 0159 CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr, 0160 LoopInfo *LI = nullptr, 0161 MemorySSAUpdater *MSSAU = nullptr, 0162 PostDominatorTree *PDT = nullptr) 0163 : DT(DT), PDT(PDT), LI(LI), MSSAU(MSSAU) {} 0164 0165 CriticalEdgeSplittingOptions &setMergeIdenticalEdges() { 0166 MergeIdenticalEdges = true; 0167 return *this; 0168 } 0169 0170 CriticalEdgeSplittingOptions &setKeepOneInputPHIs() { 0171 KeepOneInputPHIs = true; 0172 return *this; 0173 } 0174 0175 CriticalEdgeSplittingOptions &setPreserveLCSSA() { 0176 PreserveLCSSA = true; 0177 return *this; 0178 } 0179 0180 CriticalEdgeSplittingOptions &setIgnoreUnreachableDests() { 0181 IgnoreUnreachableDests = true; 0182 return *this; 0183 } 0184 0185 CriticalEdgeSplittingOptions &unsetPreserveLoopSimplify() { 0186 PreserveLoopSimplify = false; 0187 return *this; 0188 } 0189 }; 0190 0191 /// When a loop exit edge is split, LCSSA form may require new PHIs in the new 0192 /// exit block. This function inserts the new PHIs, as needed. Preds is a list 0193 /// of preds inside the loop, SplitBB is the new loop exit block, and DestBB is 0194 /// the old loop exit, now the successor of SplitBB. 0195 void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds, 0196 BasicBlock *SplitBB, BasicBlock *DestBB); 0197 0198 /// If this edge is a critical edge, insert a new node to split the critical 0199 /// edge. This will update the analyses passed in through the option struct. 0200 /// This returns the new block if the edge was split, null otherwise. 0201 /// 0202 /// If MergeIdenticalEdges in the options struct is true (not the default), 0203 /// *all* edges from TI to the specified successor will be merged into the same 0204 /// critical edge block. This is most commonly interesting with switch 0205 /// instructions, which may have many edges to any one destination. This 0206 /// ensures that all edges to that dest go to one block instead of each going 0207 /// to a different block, but isn't the standard definition of a "critical 0208 /// edge". 0209 /// 0210 /// It is invalid to call this function on a critical edge that starts at an 0211 /// IndirectBrInst. Splitting these edges will almost always create an invalid 0212 /// program because the address of the new block won't be the one that is jumped 0213 /// to. 0214 BasicBlock *SplitCriticalEdge(Instruction *TI, unsigned SuccNum, 0215 const CriticalEdgeSplittingOptions &Options = 0216 CriticalEdgeSplittingOptions(), 0217 const Twine &BBName = ""); 0218 0219 /// If it is known that an edge is critical, SplitKnownCriticalEdge can be 0220 /// called directly, rather than calling SplitCriticalEdge first. 0221 BasicBlock *SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum, 0222 const CriticalEdgeSplittingOptions &Options = 0223 CriticalEdgeSplittingOptions(), 0224 const Twine &BBName = ""); 0225 0226 /// If an edge from Src to Dst is critical, split the edge and return true, 0227 /// otherwise return false. This method requires that there be an edge between 0228 /// the two blocks. It updates the analyses passed in the options struct 0229 inline BasicBlock * 0230 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 0231 const CriticalEdgeSplittingOptions &Options = 0232 CriticalEdgeSplittingOptions()) { 0233 Instruction *TI = Src->getTerminator(); 0234 unsigned i = 0; 0235 while (true) { 0236 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 0237 if (TI->getSuccessor(i) == Dst) 0238 return SplitCriticalEdge(TI, i, Options); 0239 ++i; 0240 } 0241 } 0242 0243 /// Loop over all of the edges in the CFG, breaking critical edges as they are 0244 /// found. Returns the number of broken edges. 0245 unsigned SplitAllCriticalEdges(Function &F, 0246 const CriticalEdgeSplittingOptions &Options = 0247 CriticalEdgeSplittingOptions()); 0248 0249 /// Split the edge connecting the specified blocks, and return the newly created 0250 /// basic block between \p From and \p To. 0251 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, 0252 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr, 0253 MemorySSAUpdater *MSSAU = nullptr, 0254 const Twine &BBName = ""); 0255 0256 /// Sets the unwind edge of an instruction to a particular successor. 0257 void setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ); 0258 0259 /// Replaces all uses of OldPred with the NewPred block in all PHINodes in a 0260 /// block. 0261 void updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred, 0262 BasicBlock *NewPred, PHINode *Until = nullptr); 0263 0264 /// Split the edge connect the specficed blocks in the case that \p Succ is an 0265 /// Exception Handling Block 0266 BasicBlock *ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ, 0267 LandingPadInst *OriginalPad = nullptr, 0268 PHINode *LandingPadReplacement = nullptr, 0269 const CriticalEdgeSplittingOptions &Options = 0270 CriticalEdgeSplittingOptions(), 0271 const Twine &BBName = ""); 0272 0273 /// Split the specified block at the specified instruction. 0274 /// 0275 /// If \p Before is true, splitBlockBefore handles the block 0276 /// splitting. Otherwise, execution proceeds as described below. 0277 /// 0278 /// Everything before \p SplitPt stays in \p Old and everything starting with \p 0279 /// SplitPt moves to a new block. The two blocks are joined by an unconditional 0280 /// branch. The new block with name \p BBName is returned. 0281 /// 0282 /// FIXME: deprecated, switch to the DomTreeUpdater-based one. 0283 BasicBlock *SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, 0284 LoopInfo *LI = nullptr, 0285 MemorySSAUpdater *MSSAU = nullptr, 0286 const Twine &BBName = "", bool Before = false); 0287 inline BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT, 0288 LoopInfo *LI = nullptr, 0289 MemorySSAUpdater *MSSAU = nullptr, 0290 const Twine &BBName = "", bool Before = false) { 0291 return SplitBlock(Old, SplitPt->getIterator(), DT, LI, MSSAU, BBName, Before); 0292 } 0293 0294 /// Split the specified block at the specified instruction. 0295 /// 0296 /// If \p Before is true, splitBlockBefore handles the block 0297 /// splitting. Otherwise, execution proceeds as described below. 0298 /// 0299 /// Everything before \p SplitPt stays in \p Old and everything starting with \p 0300 /// SplitPt moves to a new block. The two blocks are joined by an unconditional 0301 /// branch. The new block with name \p BBName is returned. 0302 BasicBlock *SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, 0303 DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, 0304 MemorySSAUpdater *MSSAU = nullptr, 0305 const Twine &BBName = "", bool Before = false); 0306 inline BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, 0307 DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, 0308 MemorySSAUpdater *MSSAU = nullptr, 0309 const Twine &BBName = "", bool Before = false) { 0310 return SplitBlock(Old, SplitPt->getIterator(), DTU, LI, MSSAU, BBName, Before); 0311 } 0312 0313 /// Split the specified block at the specified instruction \p SplitPt. 0314 /// All instructions before \p SplitPt are moved to a new block and all 0315 /// instructions after \p SplitPt stay in the old block. The new block and the 0316 /// old block are joined by inserting an unconditional branch to the end of the 0317 /// new block. The new block with name \p BBName is returned. 0318 BasicBlock *splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt, 0319 DomTreeUpdater *DTU, LoopInfo *LI, 0320 MemorySSAUpdater *MSSAU, const Twine &BBName = ""); 0321 inline BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, 0322 DomTreeUpdater *DTU, LoopInfo *LI, 0323 MemorySSAUpdater *MSSAU, const Twine &BBName = "") { 0324 return splitBlockBefore(Old, SplitPt->getIterator(), DTU, LI, MSSAU, BBName); 0325 } 0326 0327 /// This method introduces at least one new basic block into the function and 0328 /// moves some of the predecessors of BB to be predecessors of the new block. 0329 /// The new predecessors are indicated by the Preds array. The new block is 0330 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors 0331 /// from Preds are now pointing. 0332 /// 0333 /// If BB is a landingpad block then additional basicblock might be introduced. 0334 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more 0335 /// details on this case. 0336 /// 0337 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 0338 /// no other analyses. In particular, it does not preserve LoopSimplify 0339 /// (because it's complicated to handle the case where one of the edges being 0340 /// split is an exit of a loop with other exits). 0341 /// 0342 /// FIXME: deprecated, switch to the DomTreeUpdater-based one. 0343 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 0344 const char *Suffix, DominatorTree *DT, 0345 LoopInfo *LI = nullptr, 0346 MemorySSAUpdater *MSSAU = nullptr, 0347 bool PreserveLCSSA = false); 0348 0349 /// This method introduces at least one new basic block into the function and 0350 /// moves some of the predecessors of BB to be predecessors of the new block. 0351 /// The new predecessors are indicated by the Preds array. The new block is 0352 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors 0353 /// from Preds are now pointing. 0354 /// 0355 /// If BB is a landingpad block then additional basicblock might be introduced. 0356 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more 0357 /// details on this case. 0358 /// 0359 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 0360 /// no other analyses. In particular, it does not preserve LoopSimplify 0361 /// (because it's complicated to handle the case where one of the edges being 0362 /// split is an exit of a loop with other exits). 0363 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 0364 const char *Suffix, 0365 DomTreeUpdater *DTU = nullptr, 0366 LoopInfo *LI = nullptr, 0367 MemorySSAUpdater *MSSAU = nullptr, 0368 bool PreserveLCSSA = false); 0369 0370 /// This method transforms the landing pad, OrigBB, by introducing two new basic 0371 /// blocks into the function. One of those new basic blocks gets the 0372 /// predecessors listed in Preds. The other basic block gets the remaining 0373 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both 0374 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and 0375 /// 'Suffix2', and are returned in the NewBBs vector. 0376 /// 0377 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 0378 /// no other analyses. In particular, it does not preserve LoopSimplify 0379 /// (because it's complicated to handle the case where one of the edges being 0380 /// split is an exit of a loop with other exits). 0381 void SplitLandingPadPredecessors( 0382 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix, 0383 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs, 0384 DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, 0385 MemorySSAUpdater *MSSAU = nullptr, bool PreserveLCSSA = false); 0386 0387 /// This method duplicates the specified return instruction into a predecessor 0388 /// which ends in an unconditional branch. If the return instruction returns a 0389 /// value defined by a PHI, propagate the right value into the return. It 0390 /// returns the new return instruction in the predecessor. 0391 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 0392 BasicBlock *Pred, 0393 DomTreeUpdater *DTU = nullptr); 0394 0395 /// Split the containing block at the specified instruction - everything before 0396 /// SplitBefore stays in the old basic block, and the rest of the instructions 0397 /// in the BB are moved to a new block. The two blocks are connected by a 0398 /// conditional branch (with value of Cmp being the condition). 0399 /// Before: 0400 /// Head 0401 /// SplitBefore 0402 /// Tail 0403 /// After: 0404 /// Head 0405 /// if (Cond) 0406 /// ThenBlock 0407 /// SplitBefore 0408 /// Tail 0409 /// 0410 /// If \p ThenBlock is not specified, a new block will be created for it. 0411 /// If \p Unreachable is true, the newly created block will end with 0412 /// UnreachableInst, otherwise it branches to Tail. 0413 /// Returns the NewBasicBlock's terminator. 0414 /// 0415 /// Updates DTU and LI if given. 0416 Instruction *SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, 0417 bool Unreachable, 0418 MDNode *BranchWeights = nullptr, 0419 DomTreeUpdater *DTU = nullptr, 0420 LoopInfo *LI = nullptr, 0421 BasicBlock *ThenBlock = nullptr); 0422 0423 inline Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, 0424 bool Unreachable, 0425 MDNode *BranchWeights = nullptr, 0426 DomTreeUpdater *DTU = nullptr, 0427 LoopInfo *LI = nullptr, 0428 BasicBlock *ThenBlock = nullptr) { 0429 return SplitBlockAndInsertIfThen(Cond, SplitBefore->getIterator(), 0430 Unreachable, BranchWeights, DTU, LI, 0431 ThenBlock); 0432 } 0433 0434 /// Similar to SplitBlockAndInsertIfThen, but the inserted block is on the false 0435 /// path of the branch. 0436 Instruction *SplitBlockAndInsertIfElse(Value *Cond, BasicBlock::iterator SplitBefore, 0437 bool Unreachable, 0438 MDNode *BranchWeights = nullptr, 0439 DomTreeUpdater *DTU = nullptr, 0440 LoopInfo *LI = nullptr, 0441 BasicBlock *ElseBlock = nullptr); 0442 0443 inline Instruction *SplitBlockAndInsertIfElse(Value *Cond, Instruction *SplitBefore, 0444 bool Unreachable, 0445 MDNode *BranchWeights = nullptr, 0446 DomTreeUpdater *DTU = nullptr, 0447 LoopInfo *LI = nullptr, 0448 BasicBlock *ElseBlock = nullptr) { 0449 return SplitBlockAndInsertIfElse(Cond, SplitBefore->getIterator(), 0450 Unreachable, BranchWeights, DTU, LI, 0451 ElseBlock); 0452 } 0453 0454 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, 0455 /// but also creates the ElseBlock. 0456 /// Before: 0457 /// Head 0458 /// SplitBefore 0459 /// Tail 0460 /// After: 0461 /// Head 0462 /// if (Cond) 0463 /// ThenBlock 0464 /// else 0465 /// ElseBlock 0466 /// SplitBefore 0467 /// Tail 0468 /// 0469 /// Updates DT if given. 0470 void SplitBlockAndInsertIfThenElse(Value *Cond, 0471 BasicBlock::iterator SplitBefore, 0472 Instruction **ThenTerm, 0473 Instruction **ElseTerm, 0474 MDNode *BranchWeights = nullptr, 0475 DomTreeUpdater *DTU = nullptr, 0476 LoopInfo *LI = nullptr); 0477 0478 inline void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 0479 Instruction **ThenTerm, 0480 Instruction **ElseTerm, 0481 MDNode *BranchWeights = nullptr, 0482 DomTreeUpdater *DTU = nullptr, 0483 LoopInfo *LI = nullptr) 0484 { 0485 SplitBlockAndInsertIfThenElse(Cond, SplitBefore->getIterator(), ThenTerm, 0486 ElseTerm, BranchWeights, DTU, LI); 0487 } 0488 0489 /// Split the containing block at the specified instruction - everything before 0490 /// SplitBefore stays in the old basic block, and the rest of the instructions 0491 /// in the BB are moved to a new block. The two blocks are connected by a 0492 /// conditional branch (with value of Cmp being the condition). 0493 /// Before: 0494 /// Head 0495 /// SplitBefore 0496 /// Tail 0497 /// After: 0498 /// Head 0499 /// if (Cond) 0500 /// TrueBlock 0501 /// else 0502 //// FalseBlock 0503 /// SplitBefore 0504 /// Tail 0505 /// 0506 /// If \p ThenBlock is null, the resulting CFG won't contain the TrueBlock. If 0507 /// \p ThenBlock is non-null and points to non-null BasicBlock pointer, that 0508 /// block will be inserted as the TrueBlock. Otherwise a new block will be 0509 /// created. Likewise for the \p ElseBlock parameter. 0510 /// If \p UnreachableThen or \p UnreachableElse is true, the corresponding newly 0511 /// created blocks will end with UnreachableInst, otherwise with branches to 0512 /// Tail. The function will not modify existing basic blocks passed to it. The 0513 /// caller must ensure that Tail is reachable from Head. 0514 /// Returns the newly created blocks in \p ThenBlock and \p ElseBlock. 0515 /// Updates DTU and LI if given. 0516 void SplitBlockAndInsertIfThenElse(Value *Cond, 0517 BasicBlock::iterator SplitBefore, 0518 BasicBlock **ThenBlock, 0519 BasicBlock **ElseBlock, 0520 bool UnreachableThen = false, 0521 bool UnreachableElse = false, 0522 MDNode *BranchWeights = nullptr, 0523 DomTreeUpdater *DTU = nullptr, 0524 LoopInfo *LI = nullptr); 0525 0526 inline void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 0527 BasicBlock **ThenBlock, 0528 BasicBlock **ElseBlock, 0529 bool UnreachableThen = false, 0530 bool UnreachableElse = false, 0531 MDNode *BranchWeights = nullptr, 0532 DomTreeUpdater *DTU = nullptr, 0533 LoopInfo *LI = nullptr) { 0534 SplitBlockAndInsertIfThenElse(Cond, SplitBefore->getIterator(), ThenBlock, 0535 ElseBlock, UnreachableThen, UnreachableElse, BranchWeights, DTU, LI); 0536 } 0537 0538 /// Insert a for (int i = 0; i < End; i++) loop structure (with the exception 0539 /// that \p End is assumed > 0, and thus not checked on entry) at \p 0540 /// SplitBefore. Returns the first insert point in the loop body, and the 0541 /// PHINode for the induction variable (i.e. "i" above). 0542 std::pair<Instruction*, Value*> 0543 SplitBlockAndInsertSimpleForLoop(Value *End, BasicBlock::iterator SplitBefore); 0544 0545 /// Utility function for performing a given action on each lane of a vector 0546 /// with \p EC elements. To simplify porting legacy code, this defaults to 0547 /// unrolling the implied loop for non-scalable element counts, but this is 0548 /// not considered to be part of the contract of this routine, and is 0549 /// expected to change in the future. The callback takes as arguments an 0550 /// IRBuilder whose insert point is correctly set for instantiating the 0551 /// given index, and a value which is (at runtime) the index to access. 0552 /// This index *may* be a constant. 0553 void SplitBlockAndInsertForEachLane( 0554 ElementCount EC, Type *IndexTy, BasicBlock::iterator InsertBefore, 0555 std::function<void(IRBuilderBase &, Value *)> Func); 0556 0557 /// Utility function for performing a given action on each lane of a vector 0558 /// with \p EVL effective length. EVL is assumed > 0. To simplify porting legacy 0559 /// code, this defaults to unrolling the implied loop for non-scalable element 0560 /// counts, but this is not considered to be part of the contract of this 0561 /// routine, and is expected to change in the future. The callback takes as 0562 /// arguments an IRBuilder whose insert point is correctly set for instantiating 0563 /// the given index, and a value which is (at runtime) the index to access. This 0564 /// index *may* be a constant. 0565 void SplitBlockAndInsertForEachLane( 0566 Value *End, BasicBlock::iterator InsertBefore, 0567 std::function<void(IRBuilderBase &, Value *)> Func); 0568 0569 /// Check whether BB is the merge point of a if-region. 0570 /// If so, return the branch instruction that determines which entry into 0571 /// BB will be taken. Also, return by references the block that will be 0572 /// entered from if the condition is true, and the block that will be 0573 /// entered if the condition is false. 0574 /// 0575 /// This does no checking to see if the true/false blocks have large or unsavory 0576 /// instructions in them. 0577 BranchInst *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 0578 BasicBlock *&IfFalse); 0579 0580 // Split critical edges where the source of the edge is an indirectbr 0581 // instruction. This isn't always possible, but we can handle some easy cases. 0582 // This is useful because MI is unable to split such critical edges, 0583 // which means it will not be able to sink instructions along those edges. 0584 // This is especially painful for indirect branches with many successors, where 0585 // we end up having to prepare all outgoing values in the origin block. 0586 // 0587 // Our normal algorithm for splitting critical edges requires us to update 0588 // the outgoing edges of the edge origin block, but for an indirectbr this 0589 // is hard, since it would require finding and updating the block addresses 0590 // the indirect branch uses. But if a block only has a single indirectbr 0591 // predecessor, with the others being regular branches, we can do it in a 0592 // different way. 0593 // Say we have A -> D, B -> D, I -> D where only I -> D is an indirectbr. 0594 // We can split D into D0 and D1, where D0 contains only the PHIs from D, 0595 // and D1 is the D block body. We can then duplicate D0 as D0A and D0B, and 0596 // create the following structure: 0597 // A -> D0A, B -> D0A, I -> D0B, D0A -> D1, D0B -> D1 0598 // If BPI and BFI aren't non-null, BPI/BFI will be updated accordingly. 0599 // When `IgnoreBlocksWithoutPHI` is set to `true` critical edges leading to a 0600 // block without phi-instructions will not be split. 0601 bool SplitIndirectBrCriticalEdges(Function &F, bool IgnoreBlocksWithoutPHI, 0602 BranchProbabilityInfo *BPI = nullptr, 0603 BlockFrequencyInfo *BFI = nullptr); 0604 0605 // Utility function for inverting branch condition and for swapping its 0606 // successors 0607 void InvertBranch(BranchInst *PBI, IRBuilderBase &Builder); 0608 0609 // Check whether the function only has simple terminator: 0610 // br/brcond/unreachable/ret 0611 bool hasOnlySimpleTerminator(const Function &F); 0612 0613 // Returns true if these basic blocks belong to a presplit coroutine and the 0614 // edge corresponds to the 'default' case in the switch statement in the 0615 // pattern: 0616 // 0617 // %0 = call i8 @llvm.coro.suspend(token none, i1 false) 0618 // switch i8 %0, label %suspend [i8 0, label %resume 0619 // i8 1, label %cleanup] 0620 // 0621 // i.e. the edge to the `%suspend` BB. This edge is special in that it will 0622 // be elided by coroutine lowering (coro-split), and the `%suspend` BB needs 0623 // to be kept as-is. It's not a real CFG edge - post-lowering, it will end 0624 // up being a `ret`, and it must be thus lowerable to support symmetric 0625 // transfer. For example: 0626 // - this edge is not a loop exit edge if encountered in a loop (and should 0627 // be ignored) 0628 // - must not be split for PGO instrumentation, for example. 0629 bool isPresplitCoroSuspendExitEdge(const BasicBlock &Src, 0630 const BasicBlock &Dest); 0631 } // end namespace llvm 0632 0633 #endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|