10b57cec5SDimitry Andric //===- MachineDominators.cpp - Machine 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 on machine functions. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 150b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 17480093f4SDimitry Andric #include "llvm/InitializePasses.h" 1881ad6265SDimitry Andric #include "llvm/Pass.h" 1981ad6265SDimitry Andric #include "llvm/PassRegistry.h" 200b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 21*0fca6ea1SDimitry Andric #include "llvm/Support/GenericDomTreeConstruction.h" 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric using namespace llvm; 240b57cec5SDimitry Andric 258bcb0991SDimitry Andric namespace llvm { 260b57cec5SDimitry Andric // Always verify dominfo if expensive checking is enabled. 270b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 288bcb0991SDimitry Andric bool VerifyMachineDomInfo = true; 290b57cec5SDimitry Andric #else 308bcb0991SDimitry Andric bool VerifyMachineDomInfo = false; 310b57cec5SDimitry Andric #endif 328bcb0991SDimitry Andric } // namespace llvm 338bcb0991SDimitry Andric 340b57cec5SDimitry Andric static cl::opt<bool, true> VerifyMachineDomInfoX( 350b57cec5SDimitry Andric "verify-machine-dom-info", cl::location(VerifyMachineDomInfo), cl::Hidden, 360b57cec5SDimitry Andric cl::desc("Verify machine dominator info (time consuming)")); 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric namespace llvm { 390b57cec5SDimitry Andric template class DomTreeNodeBase<MachineBasicBlock>; 400b57cec5SDimitry Andric template class DominatorTreeBase<MachineBasicBlock, false>; // DomTreeBase 41*0fca6ea1SDimitry Andric 42*0fca6ea1SDimitry Andric namespace DomTreeBuilder { 43*0fca6ea1SDimitry Andric template void Calculate<MBBDomTree>(MBBDomTree &DT); 44*0fca6ea1SDimitry Andric template void CalculateWithUpdates<MBBDomTree>(MBBDomTree &DT, MBBUpdates U); 45*0fca6ea1SDimitry Andric 46*0fca6ea1SDimitry Andric template void InsertEdge<MBBDomTree>(MBBDomTree &DT, MachineBasicBlock *From, 47*0fca6ea1SDimitry Andric MachineBasicBlock *To); 48*0fca6ea1SDimitry Andric 49*0fca6ea1SDimitry Andric template void DeleteEdge<MBBDomTree>(MBBDomTree &DT, MachineBasicBlock *From, 50*0fca6ea1SDimitry Andric MachineBasicBlock *To); 51*0fca6ea1SDimitry Andric 52*0fca6ea1SDimitry Andric template void ApplyUpdates<MBBDomTree>(MBBDomTree &DT, MBBDomTreeGraphDiff &, 53*0fca6ea1SDimitry Andric MBBDomTreeGraphDiff *); 54*0fca6ea1SDimitry Andric 55*0fca6ea1SDimitry Andric template bool Verify<MBBDomTree>(const MBBDomTree &DT, 56*0fca6ea1SDimitry Andric MBBDomTree::VerificationLevel VL); 57*0fca6ea1SDimitry Andric } // namespace DomTreeBuilder 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 60*0fca6ea1SDimitry Andric bool MachineDominatorTree::invalidate( 61*0fca6ea1SDimitry Andric MachineFunction &, const PreservedAnalyses &PA, 62*0fca6ea1SDimitry Andric MachineFunctionAnalysisManager::Invalidator &) { 63*0fca6ea1SDimitry Andric // Check whether the analysis, all analyses on machine functions, or the 64*0fca6ea1SDimitry Andric // machine function's CFG have been preserved. 65*0fca6ea1SDimitry Andric auto PAC = PA.getChecker<MachineDominatorTreeAnalysis>(); 66*0fca6ea1SDimitry Andric return !PAC.preserved() && 67*0fca6ea1SDimitry Andric !PAC.preservedSet<AllAnalysesOn<MachineFunction>>() && 68*0fca6ea1SDimitry Andric !PAC.preservedSet<CFGAnalyses>(); 69*0fca6ea1SDimitry Andric } 700b57cec5SDimitry Andric 71*0fca6ea1SDimitry Andric AnalysisKey MachineDominatorTreeAnalysis::Key; 72*0fca6ea1SDimitry Andric 73*0fca6ea1SDimitry Andric MachineDominatorTreeAnalysis::Result 74*0fca6ea1SDimitry Andric MachineDominatorTreeAnalysis::run(MachineFunction &MF, 75*0fca6ea1SDimitry Andric MachineFunctionAnalysisManager &) { 76*0fca6ea1SDimitry Andric return MachineDominatorTree(MF); 77*0fca6ea1SDimitry Andric } 78*0fca6ea1SDimitry Andric 79*0fca6ea1SDimitry Andric PreservedAnalyses 80*0fca6ea1SDimitry Andric MachineDominatorTreePrinterPass::run(MachineFunction &MF, 81*0fca6ea1SDimitry Andric MachineFunctionAnalysisManager &MFAM) { 82*0fca6ea1SDimitry Andric OS << "MachineDominatorTree for machine function: " << MF.getName() << '\n'; 83*0fca6ea1SDimitry Andric MFAM.getResult<MachineDominatorTreeAnalysis>(MF).print(OS); 84*0fca6ea1SDimitry Andric return PreservedAnalyses::all(); 85*0fca6ea1SDimitry Andric } 86*0fca6ea1SDimitry Andric 87*0fca6ea1SDimitry Andric char MachineDominatorTreeWrapperPass::ID = 0; 88*0fca6ea1SDimitry Andric 89*0fca6ea1SDimitry Andric INITIALIZE_PASS(MachineDominatorTreeWrapperPass, "machinedomtree", 900b57cec5SDimitry Andric "MachineDominator Tree Construction", true, true) 910b57cec5SDimitry Andric 92*0fca6ea1SDimitry Andric MachineDominatorTreeWrapperPass::MachineDominatorTreeWrapperPass() 93*0fca6ea1SDimitry Andric : MachineFunctionPass(ID) { 94*0fca6ea1SDimitry Andric initializeMachineDominatorTreeWrapperPassPass( 95*0fca6ea1SDimitry Andric *PassRegistry::getPassRegistry()); 96480093f4SDimitry Andric } 97480093f4SDimitry Andric 98480093f4SDimitry Andric void MachineDominatorTree::calculate(MachineFunction &F) { 990b57cec5SDimitry Andric CriticalEdgesToSplit.clear(); 1000b57cec5SDimitry Andric NewBBs.clear(); 101*0fca6ea1SDimitry Andric recalculate(F); 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric 104*0fca6ea1SDimitry Andric char &llvm::MachineDominatorsID = MachineDominatorTreeWrapperPass::ID; 105*0fca6ea1SDimitry Andric 106*0fca6ea1SDimitry Andric bool MachineDominatorTreeWrapperPass::runOnMachineFunction(MachineFunction &F) { 107*0fca6ea1SDimitry Andric DT = MachineDominatorTree(F); 108*0fca6ea1SDimitry Andric return false; 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 111*0fca6ea1SDimitry Andric void MachineDominatorTreeWrapperPass::releaseMemory() { DT.reset(); } 112*0fca6ea1SDimitry Andric 113*0fca6ea1SDimitry Andric void MachineDominatorTreeWrapperPass::verifyAnalysis() const { 114*0fca6ea1SDimitry Andric if (VerifyMachineDomInfo && DT) 115*0fca6ea1SDimitry Andric if (!DT->verify(MachineDominatorTree::VerificationLevel::Basic)) 116*0fca6ea1SDimitry Andric report_fatal_error("MachineDominatorTree verification failed!"); 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric 119*0fca6ea1SDimitry Andric void MachineDominatorTreeWrapperPass::print(raw_ostream &OS, 120*0fca6ea1SDimitry Andric const Module *) const { 1210b57cec5SDimitry Andric if (DT) 1220b57cec5SDimitry Andric DT->print(OS); 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric void MachineDominatorTree::applySplitCriticalEdges() const { 1260b57cec5SDimitry Andric // Bail out early if there is nothing to do. 1270b57cec5SDimitry Andric if (CriticalEdgesToSplit.empty()) 1280b57cec5SDimitry Andric return; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric // For each element in CriticalEdgesToSplit, remember whether or not element 1310b57cec5SDimitry Andric // is the new immediate domminator of its successor. The mapping is done by 1320b57cec5SDimitry Andric // index, i.e., the information for the ith element of CriticalEdgesToSplit is 1330b57cec5SDimitry Andric // the ith element of IsNewIDom. 1340b57cec5SDimitry Andric SmallBitVector IsNewIDom(CriticalEdgesToSplit.size(), true); 1350b57cec5SDimitry Andric size_t Idx = 0; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric // Collect all the dominance properties info, before invalidating 1380b57cec5SDimitry Andric // the underlying DT. 1390b57cec5SDimitry Andric for (CriticalEdge &Edge : CriticalEdgesToSplit) { 1400b57cec5SDimitry Andric // Update dominator information. 1410b57cec5SDimitry Andric MachineBasicBlock *Succ = Edge.ToBB; 142*0fca6ea1SDimitry Andric MachineDomTreeNode *SuccDTNode = Base::getNode(Succ); 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric for (MachineBasicBlock *PredBB : Succ->predecessors()) { 1450b57cec5SDimitry Andric if (PredBB == Edge.NewBB) 1460b57cec5SDimitry Andric continue; 1470b57cec5SDimitry Andric // If we are in this situation: 1480b57cec5SDimitry Andric // FromBB1 FromBB2 1490b57cec5SDimitry Andric // + + 1500b57cec5SDimitry Andric // + + + + 1510b57cec5SDimitry Andric // + + + + 1520b57cec5SDimitry Andric // ... Split1 Split2 ... 1530b57cec5SDimitry Andric // + + 1540b57cec5SDimitry Andric // + + 1550b57cec5SDimitry Andric // + 1560b57cec5SDimitry Andric // Succ 1570b57cec5SDimitry Andric // Instead of checking the domiance property with Split2, we check it with 1580b57cec5SDimitry Andric // FromBB2 since Split2 is still unknown of the underlying DT structure. 1590b57cec5SDimitry Andric if (NewBBs.count(PredBB)) { 1600b57cec5SDimitry Andric assert(PredBB->pred_size() == 1 && "A basic block resulting from a " 1610b57cec5SDimitry Andric "critical edge split has more " 1620b57cec5SDimitry Andric "than one predecessor!"); 1630b57cec5SDimitry Andric PredBB = *PredBB->pred_begin(); 1640b57cec5SDimitry Andric } 165*0fca6ea1SDimitry Andric if (!Base::dominates(SuccDTNode, Base::getNode(PredBB))) { 1660b57cec5SDimitry Andric IsNewIDom[Idx] = false; 1670b57cec5SDimitry Andric break; 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric ++Idx; 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // Now, update DT with the collected dominance properties info. 1740b57cec5SDimitry Andric Idx = 0; 1750b57cec5SDimitry Andric for (CriticalEdge &Edge : CriticalEdgesToSplit) { 1760b57cec5SDimitry Andric // We know FromBB dominates NewBB. 177*0fca6ea1SDimitry Andric MachineDomTreeNode *NewDTNode = 178*0fca6ea1SDimitry Andric const_cast<MachineDominatorTree *>(this)->Base::addNewBlock( 179*0fca6ea1SDimitry Andric Edge.NewBB, Edge.FromBB); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric // If all the other predecessors of "Succ" are dominated by "Succ" itself 1820b57cec5SDimitry Andric // then the new block is the new immediate dominator of "Succ". Otherwise, 1830b57cec5SDimitry Andric // the new block doesn't dominate anything. 1840b57cec5SDimitry Andric if (IsNewIDom[Idx]) 185*0fca6ea1SDimitry Andric const_cast<MachineDominatorTree *>(this)->Base::changeImmediateDominator( 186*0fca6ea1SDimitry Andric Base::getNode(Edge.ToBB), NewDTNode); 1870b57cec5SDimitry Andric ++Idx; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric NewBBs.clear(); 1900b57cec5SDimitry Andric CriticalEdgesToSplit.clear(); 1910b57cec5SDimitry Andric } 192