10b57cec5SDimitry Andric //===- Dominators.cpp - Dominator Calculation -----------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements simple dominator construction algorithms for finding 100b57cec5SDimitry Andric // forward dominators. Postdominators are available in libanalysis, but are not 110b57cec5SDimitry Andric // included in libvmcore, because it's not needed. Forward dominators are 120b57cec5SDimitry Andric // needed to support the Verifier pass. 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 171fd87a68SDimitry Andric #include "llvm/ADT/StringRef.h" 180b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 190b57cec5SDimitry Andric #include "llvm/IR/CFG.h" 201fd87a68SDimitry Andric #include "llvm/IR/Function.h" 211fd87a68SDimitry Andric #include "llvm/IR/Instruction.h" 220b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 230b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 24480093f4SDimitry Andric #include "llvm/InitializePasses.h" 251fd87a68SDimitry Andric #include "llvm/PassRegistry.h" 261fd87a68SDimitry Andric #include "llvm/Support/Casting.h" 270b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 285f757f3fSDimitry Andric #include "llvm/Support/GenericDomTreeConstruction.h" 290b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 301fd87a68SDimitry Andric 311fd87a68SDimitry Andric #include <cassert> 321fd87a68SDimitry Andric 331fd87a68SDimitry Andric namespace llvm { 341fd87a68SDimitry Andric class Argument; 351fd87a68SDimitry Andric class Constant; 361fd87a68SDimitry Andric class Value; 371fd87a68SDimitry Andric } // namespace llvm 380b57cec5SDimitry Andric using namespace llvm; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric bool llvm::VerifyDomInfo = false; 410b57cec5SDimitry Andric static cl::opt<bool, true> 420b57cec5SDimitry Andric VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo), cl::Hidden, 430b57cec5SDimitry Andric cl::desc("Verify dominator info (time consuming)")); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 460b57cec5SDimitry Andric static constexpr bool ExpensiveChecksEnabled = true; 470b57cec5SDimitry Andric #else 480b57cec5SDimitry Andric static constexpr bool ExpensiveChecksEnabled = false; 490b57cec5SDimitry Andric #endif 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric bool BasicBlockEdge::isSingleEdge() const { 520b57cec5SDimitry Andric unsigned NumEdgesToEnd = 0; 53*7a6dacacSDimitry Andric for (const BasicBlock *Succ : successors(Start)) { 54*7a6dacacSDimitry Andric if (Succ == End) 550b57cec5SDimitry Andric ++NumEdgesToEnd; 560b57cec5SDimitry Andric if (NumEdgesToEnd >= 2) 570b57cec5SDimitry Andric return false; 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric assert(NumEdgesToEnd == 1); 600b57cec5SDimitry Andric return true; 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 640b57cec5SDimitry Andric // DominatorTree Implementation 650b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 660b57cec5SDimitry Andric // 670b57cec5SDimitry Andric // Provide public access to DominatorTree information. Implementation details 680b57cec5SDimitry Andric // can be found in Dominators.h, GenericDomTree.h, and 690b57cec5SDimitry Andric // GenericDomTreeConstruction.h. 700b57cec5SDimitry Andric // 710b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric template class llvm::DomTreeNodeBase<BasicBlock>; 740b57cec5SDimitry Andric template class llvm::DominatorTreeBase<BasicBlock, false>; // DomTreeBase 750b57cec5SDimitry Andric template class llvm::DominatorTreeBase<BasicBlock, true>; // PostDomTreeBase 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric template class llvm::cfg::Update<BasicBlock *>; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBDomTree>( 800b57cec5SDimitry Andric DomTreeBuilder::BBDomTree &DT); 810b57cec5SDimitry Andric template void 820b57cec5SDimitry Andric llvm::DomTreeBuilder::CalculateWithUpdates<DomTreeBuilder::BBDomTree>( 830b57cec5SDimitry Andric DomTreeBuilder::BBDomTree &DT, BBUpdates U); 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBPostDomTree>( 860b57cec5SDimitry Andric DomTreeBuilder::BBPostDomTree &DT); 870b57cec5SDimitry Andric // No CalculateWithUpdates<PostDomTree> instantiation, unless a usecase arises. 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBDomTree>( 900b57cec5SDimitry Andric DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To); 910b57cec5SDimitry Andric template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBPostDomTree>( 920b57cec5SDimitry Andric DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBDomTree>( 950b57cec5SDimitry Andric DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To); 960b57cec5SDimitry Andric template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBPostDomTree>( 970b57cec5SDimitry Andric DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To); 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBDomTree>( 100e8d8bef9SDimitry Andric DomTreeBuilder::BBDomTree &DT, DomTreeBuilder::BBDomTreeGraphDiff &, 101e8d8bef9SDimitry Andric DomTreeBuilder::BBDomTreeGraphDiff *); 1020b57cec5SDimitry Andric template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBPostDomTree>( 103e8d8bef9SDimitry Andric DomTreeBuilder::BBPostDomTree &DT, DomTreeBuilder::BBPostDomTreeGraphDiff &, 104e8d8bef9SDimitry Andric DomTreeBuilder::BBPostDomTreeGraphDiff *); 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBDomTree>( 1070b57cec5SDimitry Andric const DomTreeBuilder::BBDomTree &DT, 1080b57cec5SDimitry Andric DomTreeBuilder::BBDomTree::VerificationLevel VL); 1090b57cec5SDimitry Andric template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBPostDomTree>( 1100b57cec5SDimitry Andric const DomTreeBuilder::BBPostDomTree &DT, 1110b57cec5SDimitry Andric DomTreeBuilder::BBPostDomTree::VerificationLevel VL); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric bool DominatorTree::invalidate(Function &F, const PreservedAnalyses &PA, 1140b57cec5SDimitry Andric FunctionAnalysisManager::Invalidator &) { 1150b57cec5SDimitry Andric // Check whether the analysis, all analyses on functions, or the function's 1160b57cec5SDimitry Andric // CFG have been preserved. 1170b57cec5SDimitry Andric auto PAC = PA.getChecker<DominatorTreeAnalysis>(); 1180b57cec5SDimitry Andric return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() || 1190b57cec5SDimitry Andric PAC.preservedSet<CFGAnalyses>()); 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 122fe6060f1SDimitry Andric bool DominatorTree::dominates(const BasicBlock *BB, const Use &U) const { 123fe6060f1SDimitry Andric Instruction *UserInst = cast<Instruction>(U.getUser()); 124fe6060f1SDimitry Andric if (auto *PN = dyn_cast<PHINode>(UserInst)) 125fe6060f1SDimitry Andric // A phi use using a value from a block is dominated by the end of that 126fe6060f1SDimitry Andric // block. Note that the phi's parent block may not be. 127fe6060f1SDimitry Andric return dominates(BB, PN->getIncomingBlock(U)); 128fe6060f1SDimitry Andric else 129fe6060f1SDimitry Andric return properlyDominates(BB, UserInst->getParent()); 130fe6060f1SDimitry Andric } 131fe6060f1SDimitry Andric 1320b57cec5SDimitry Andric // dominates - Return true if Def dominates a use in User. This performs 1330b57cec5SDimitry Andric // the special checks necessary if Def and User are in the same basic block. 1340b57cec5SDimitry Andric // Note that Def doesn't dominate a use in Def itself! 135e8d8bef9SDimitry Andric bool DominatorTree::dominates(const Value *DefV, 1360b57cec5SDimitry Andric const Instruction *User) const { 137e8d8bef9SDimitry Andric const Instruction *Def = dyn_cast<Instruction>(DefV); 138e8d8bef9SDimitry Andric if (!Def) { 139e8d8bef9SDimitry Andric assert((isa<Argument>(DefV) || isa<Constant>(DefV)) && 140e8d8bef9SDimitry Andric "Should be called with an instruction, argument or constant"); 141e8d8bef9SDimitry Andric return true; // Arguments and constants dominate everything. 142e8d8bef9SDimitry Andric } 143e8d8bef9SDimitry Andric 1440b57cec5SDimitry Andric const BasicBlock *UseBB = User->getParent(); 1450b57cec5SDimitry Andric const BasicBlock *DefBB = Def->getParent(); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric // Any unreachable use is dominated, even if Def == User. 1480b57cec5SDimitry Andric if (!isReachableFromEntry(UseBB)) 1490b57cec5SDimitry Andric return true; 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric // Unreachable definitions don't dominate anything. 1520b57cec5SDimitry Andric if (!isReachableFromEntry(DefBB)) 1530b57cec5SDimitry Andric return false; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric // An instruction doesn't dominate a use in itself. 1560b57cec5SDimitry Andric if (Def == User) 1570b57cec5SDimitry Andric return false; 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric // The value defined by an invoke dominates an instruction only if it 1600b57cec5SDimitry Andric // dominates every instruction in UseBB. 1610b57cec5SDimitry Andric // A PHI is dominated only if the instruction dominates every possible use in 1620b57cec5SDimitry Andric // the UseBB. 1635ffd83dbSDimitry Andric if (isa<InvokeInst>(Def) || isa<CallBrInst>(Def) || isa<PHINode>(User)) 1640b57cec5SDimitry Andric return dominates(Def, UseBB); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric if (DefBB != UseBB) 1670b57cec5SDimitry Andric return dominates(DefBB, UseBB); 1680b57cec5SDimitry Andric 1695ffd83dbSDimitry Andric return Def->comesBefore(User); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // true if Def would dominate a use in any instruction in UseBB. 1730b57cec5SDimitry Andric // note that dominates(Def, Def->getParent()) is false. 1740b57cec5SDimitry Andric bool DominatorTree::dominates(const Instruction *Def, 1750b57cec5SDimitry Andric const BasicBlock *UseBB) const { 1760b57cec5SDimitry Andric const BasicBlock *DefBB = Def->getParent(); 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric // Any unreachable use is dominated, even if DefBB == UseBB. 1790b57cec5SDimitry Andric if (!isReachableFromEntry(UseBB)) 1800b57cec5SDimitry Andric return true; 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric // Unreachable definitions don't dominate anything. 1830b57cec5SDimitry Andric if (!isReachableFromEntry(DefBB)) 1840b57cec5SDimitry Andric return false; 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric if (DefBB == UseBB) 1870b57cec5SDimitry Andric return false; 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric // Invoke results are only usable in the normal destination, not in the 1900b57cec5SDimitry Andric // exceptional destination. 1910b57cec5SDimitry Andric if (const auto *II = dyn_cast<InvokeInst>(Def)) { 1920b57cec5SDimitry Andric BasicBlock *NormalDest = II->getNormalDest(); 1930b57cec5SDimitry Andric BasicBlockEdge E(DefBB, NormalDest); 1940b57cec5SDimitry Andric return dominates(E, UseBB); 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric return dominates(DefBB, UseBB); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric bool DominatorTree::dominates(const BasicBlockEdge &BBE, 2010b57cec5SDimitry Andric const BasicBlock *UseBB) const { 2020b57cec5SDimitry Andric // If the BB the edge ends in doesn't dominate the use BB, then the 2030b57cec5SDimitry Andric // edge also doesn't. 2040b57cec5SDimitry Andric const BasicBlock *Start = BBE.getStart(); 2050b57cec5SDimitry Andric const BasicBlock *End = BBE.getEnd(); 2060b57cec5SDimitry Andric if (!dominates(End, UseBB)) 2070b57cec5SDimitry Andric return false; 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // Simple case: if the end BB has a single predecessor, the fact that it 2100b57cec5SDimitry Andric // dominates the use block implies that the edge also does. 2110b57cec5SDimitry Andric if (End->getSinglePredecessor()) 2120b57cec5SDimitry Andric return true; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // The normal edge from the invoke is critical. Conceptually, what we would 2150b57cec5SDimitry Andric // like to do is split it and check if the new block dominates the use. 2160b57cec5SDimitry Andric // With X being the new block, the graph would look like: 2170b57cec5SDimitry Andric // 2180b57cec5SDimitry Andric // DefBB 2190b57cec5SDimitry Andric // /\ . . 2200b57cec5SDimitry Andric // / \ . . 2210b57cec5SDimitry Andric // / \ . . 2220b57cec5SDimitry Andric // / \ | | 2230b57cec5SDimitry Andric // A X B C 2240b57cec5SDimitry Andric // | \ | / 2250b57cec5SDimitry Andric // . \|/ 2260b57cec5SDimitry Andric // . NormalDest 2270b57cec5SDimitry Andric // . 2280b57cec5SDimitry Andric // 2290b57cec5SDimitry Andric // Given the definition of dominance, NormalDest is dominated by X iff X 2300b57cec5SDimitry Andric // dominates all of NormalDest's predecessors (X, B, C in the example). X 2310b57cec5SDimitry Andric // trivially dominates itself, so we only have to find if it dominates the 2320b57cec5SDimitry Andric // other predecessors. Since the only way out of X is via NormalDest, X can 2330b57cec5SDimitry Andric // only properly dominate a node if NormalDest dominates that node too. 2340b57cec5SDimitry Andric int IsDuplicateEdge = 0; 235fe6060f1SDimitry Andric for (const BasicBlock *BB : predecessors(End)) { 2360b57cec5SDimitry Andric if (BB == Start) { 2370b57cec5SDimitry Andric // If there are multiple edges between Start and End, by definition they 2380b57cec5SDimitry Andric // can't dominate anything. 2390b57cec5SDimitry Andric if (IsDuplicateEdge++) 2400b57cec5SDimitry Andric return false; 2410b57cec5SDimitry Andric continue; 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric if (!dominates(End, BB)) 2450b57cec5SDimitry Andric return false; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric return true; 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric bool DominatorTree::dominates(const BasicBlockEdge &BBE, const Use &U) const { 2510b57cec5SDimitry Andric Instruction *UserInst = cast<Instruction>(U.getUser()); 2520b57cec5SDimitry Andric // A PHI in the end of the edge is dominated by it. 2530b57cec5SDimitry Andric PHINode *PN = dyn_cast<PHINode>(UserInst); 2540b57cec5SDimitry Andric if (PN && PN->getParent() == BBE.getEnd() && 2550b57cec5SDimitry Andric PN->getIncomingBlock(U) == BBE.getStart()) 2560b57cec5SDimitry Andric return true; 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric // Otherwise use the edge-dominates-block query, which 2590b57cec5SDimitry Andric // handles the crazy critical edge cases properly. 2600b57cec5SDimitry Andric const BasicBlock *UseBB; 2610b57cec5SDimitry Andric if (PN) 2620b57cec5SDimitry Andric UseBB = PN->getIncomingBlock(U); 2630b57cec5SDimitry Andric else 2640b57cec5SDimitry Andric UseBB = UserInst->getParent(); 2650b57cec5SDimitry Andric return dominates(BBE, UseBB); 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 268e8d8bef9SDimitry Andric bool DominatorTree::dominates(const Value *DefV, const Use &U) const { 269e8d8bef9SDimitry Andric const Instruction *Def = dyn_cast<Instruction>(DefV); 270e8d8bef9SDimitry Andric if (!Def) { 271e8d8bef9SDimitry Andric assert((isa<Argument>(DefV) || isa<Constant>(DefV)) && 272e8d8bef9SDimitry Andric "Should be called with an instruction, argument or constant"); 273e8d8bef9SDimitry Andric return true; // Arguments and constants dominate everything. 274e8d8bef9SDimitry Andric } 275e8d8bef9SDimitry Andric 2760b57cec5SDimitry Andric Instruction *UserInst = cast<Instruction>(U.getUser()); 2770b57cec5SDimitry Andric const BasicBlock *DefBB = Def->getParent(); 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric // Determine the block in which the use happens. PHI nodes use 2800b57cec5SDimitry Andric // their operands on edges; simulate this by thinking of the use 2810b57cec5SDimitry Andric // happening at the end of the predecessor block. 2820b57cec5SDimitry Andric const BasicBlock *UseBB; 2830b57cec5SDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(UserInst)) 2840b57cec5SDimitry Andric UseBB = PN->getIncomingBlock(U); 2850b57cec5SDimitry Andric else 2860b57cec5SDimitry Andric UseBB = UserInst->getParent(); 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // Any unreachable use is dominated, even if Def == User. 2890b57cec5SDimitry Andric if (!isReachableFromEntry(UseBB)) 2900b57cec5SDimitry Andric return true; 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // Unreachable definitions don't dominate anything. 2930b57cec5SDimitry Andric if (!isReachableFromEntry(DefBB)) 2940b57cec5SDimitry Andric return false; 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // Invoke instructions define their return values on the edges to their normal 2970b57cec5SDimitry Andric // successors, so we have to handle them specially. 2980b57cec5SDimitry Andric // Among other things, this means they don't dominate anything in 2990b57cec5SDimitry Andric // their own block, except possibly a phi, so we don't need to 3000b57cec5SDimitry Andric // walk the block in any case. 3010b57cec5SDimitry Andric if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) { 3020b57cec5SDimitry Andric BasicBlock *NormalDest = II->getNormalDest(); 3030b57cec5SDimitry Andric BasicBlockEdge E(DefBB, NormalDest); 3040b57cec5SDimitry Andric return dominates(E, U); 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric // If the def and use are in different blocks, do a simple CFG dominator 3080b57cec5SDimitry Andric // tree query. 3090b57cec5SDimitry Andric if (DefBB != UseBB) 3100b57cec5SDimitry Andric return dominates(DefBB, UseBB); 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric // Ok, def and use are in the same block. If the def is an invoke, it 3130b57cec5SDimitry Andric // doesn't dominate anything in the block. If it's a PHI, it dominates 3140b57cec5SDimitry Andric // everything in the block. 3150b57cec5SDimitry Andric if (isa<PHINode>(UserInst)) 3160b57cec5SDimitry Andric return true; 3170b57cec5SDimitry Andric 3185ffd83dbSDimitry Andric return Def->comesBefore(UserInst); 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric bool DominatorTree::isReachableFromEntry(const Use &U) const { 3220b57cec5SDimitry Andric Instruction *I = dyn_cast<Instruction>(U.getUser()); 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric // ConstantExprs aren't really reachable from the entry block, but they 3250b57cec5SDimitry Andric // don't need to be treated like unreachable code either. 3260b57cec5SDimitry Andric if (!I) return true; 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric // PHI nodes use their operands on their incoming edges. 3290b57cec5SDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(I)) 3300b57cec5SDimitry Andric return isReachableFromEntry(PN->getIncomingBlock(U)); 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric // Everything else uses their operands in their own block. 3330b57cec5SDimitry Andric return isReachableFromEntry(I->getParent()); 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 3365ffd83dbSDimitry Andric // Edge BBE1 dominates edge BBE2 if they match or BBE1 dominates start of BBE2. 3375ffd83dbSDimitry Andric bool DominatorTree::dominates(const BasicBlockEdge &BBE1, 3385ffd83dbSDimitry Andric const BasicBlockEdge &BBE2) const { 3395ffd83dbSDimitry Andric if (BBE1.getStart() == BBE2.getStart() && BBE1.getEnd() == BBE2.getEnd()) 3405ffd83dbSDimitry Andric return true; 3415ffd83dbSDimitry Andric return dominates(BBE1, BBE2.getStart()); 3425ffd83dbSDimitry Andric } 3435ffd83dbSDimitry Andric 344bdd1243dSDimitry Andric Instruction *DominatorTree::findNearestCommonDominator(Instruction *I1, 345bdd1243dSDimitry Andric Instruction *I2) const { 346bdd1243dSDimitry Andric BasicBlock *BB1 = I1->getParent(); 347bdd1243dSDimitry Andric BasicBlock *BB2 = I2->getParent(); 348bdd1243dSDimitry Andric if (BB1 == BB2) 349bdd1243dSDimitry Andric return I1->comesBefore(I2) ? I1 : I2; 350bdd1243dSDimitry Andric if (!isReachableFromEntry(BB2)) 351bdd1243dSDimitry Andric return I1; 352bdd1243dSDimitry Andric if (!isReachableFromEntry(BB1)) 353bdd1243dSDimitry Andric return I2; 354bdd1243dSDimitry Andric BasicBlock *DomBB = findNearestCommonDominator(BB1, BB2); 355bdd1243dSDimitry Andric if (BB1 == DomBB) 356bdd1243dSDimitry Andric return I1; 357bdd1243dSDimitry Andric if (BB2 == DomBB) 358bdd1243dSDimitry Andric return I2; 359bdd1243dSDimitry Andric return DomBB->getTerminator(); 360bdd1243dSDimitry Andric } 361bdd1243dSDimitry Andric 3620b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3630b57cec5SDimitry Andric // DominatorTreeAnalysis and related pass implementations 3640b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3650b57cec5SDimitry Andric // 3660b57cec5SDimitry Andric // This implements the DominatorTreeAnalysis which is used with the new pass 3670b57cec5SDimitry Andric // manager. It also implements some methods from utility passes. 3680b57cec5SDimitry Andric // 3690b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric DominatorTree DominatorTreeAnalysis::run(Function &F, 3720b57cec5SDimitry Andric FunctionAnalysisManager &) { 3730b57cec5SDimitry Andric DominatorTree DT; 3740b57cec5SDimitry Andric DT.recalculate(F); 3750b57cec5SDimitry Andric return DT; 3760b57cec5SDimitry Andric } 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric AnalysisKey DominatorTreeAnalysis::Key; 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric DominatorTreePrinterPass::DominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {} 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric PreservedAnalyses DominatorTreePrinterPass::run(Function &F, 3830b57cec5SDimitry Andric FunctionAnalysisManager &AM) { 3840b57cec5SDimitry Andric OS << "DominatorTree for function: " << F.getName() << "\n"; 3850b57cec5SDimitry Andric AM.getResult<DominatorTreeAnalysis>(F).print(OS); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric return PreservedAnalyses::all(); 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric PreservedAnalyses DominatorTreeVerifierPass::run(Function &F, 3910b57cec5SDimitry Andric FunctionAnalysisManager &AM) { 3920b57cec5SDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 3930b57cec5SDimitry Andric assert(DT.verify()); 3940b57cec5SDimitry Andric (void)DT; 3950b57cec5SDimitry Andric return PreservedAnalyses::all(); 3960b57cec5SDimitry Andric } 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3990b57cec5SDimitry Andric // DominatorTreeWrapperPass Implementation 4000b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4010b57cec5SDimitry Andric // 4020b57cec5SDimitry Andric // The implementation details of the wrapper pass that holds a DominatorTree 4030b57cec5SDimitry Andric // suitable for use with the legacy pass manager. 4040b57cec5SDimitry Andric // 4050b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric char DominatorTreeWrapperPass::ID = 0; 408480093f4SDimitry Andric 409480093f4SDimitry Andric DominatorTreeWrapperPass::DominatorTreeWrapperPass() : FunctionPass(ID) { 410480093f4SDimitry Andric initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry()); 411480093f4SDimitry Andric } 412480093f4SDimitry Andric 4130b57cec5SDimitry Andric INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree", 4140b57cec5SDimitry Andric "Dominator Tree Construction", true, true) 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric bool DominatorTreeWrapperPass::runOnFunction(Function &F) { 4170b57cec5SDimitry Andric DT.recalculate(F); 4180b57cec5SDimitry Andric return false; 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric void DominatorTreeWrapperPass::verifyAnalysis() const { 4220b57cec5SDimitry Andric if (VerifyDomInfo) 4230b57cec5SDimitry Andric assert(DT.verify(DominatorTree::VerificationLevel::Full)); 4240b57cec5SDimitry Andric else if (ExpensiveChecksEnabled) 4250b57cec5SDimitry Andric assert(DT.verify(DominatorTree::VerificationLevel::Basic)); 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const { 4290b57cec5SDimitry Andric DT.print(OS); 4300b57cec5SDimitry Andric } 431