[LoongArch] Eliminate the redundant sign extension of division (#107971)
[llvm-project.git] / llvm / lib / CodeGen / MachineRegionInfo.cpp
blobf8268b8894ca3768cc0bc7f160796752de3c9088
1 //===- lib/Codegen/MachineRegionInfo.cpp ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineRegionInfo.h"
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/Analysis/RegionInfoImpl.h"
12 #include "llvm/CodeGen/MachinePostDominators.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/InitializePasses.h"
15 #include "llvm/Pass.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Debug.h"
19 #define DEBUG_TYPE "machine-region-info"
21 using namespace llvm;
23 STATISTIC(numMachineRegions, "The # of machine regions");
24 STATISTIC(numMachineSimpleRegions, "The # of simple machine regions");
26 namespace llvm {
28 template class RegionBase<RegionTraits<MachineFunction>>;
29 template class RegionNodeBase<RegionTraits<MachineFunction>>;
30 template class RegionInfoBase<RegionTraits<MachineFunction>>;
32 } // end namespace llvm
34 //===----------------------------------------------------------------------===//
35 // MachineRegion implementation
37 MachineRegion::MachineRegion(MachineBasicBlock *Entry, MachineBasicBlock *Exit,
38 MachineRegionInfo* RI,
39 MachineDominatorTree *DT, MachineRegion *Parent) :
40 RegionBase<RegionTraits<MachineFunction>>(Entry, Exit, RI, DT, Parent) {}
42 MachineRegion::~MachineRegion() = default;
44 //===----------------------------------------------------------------------===//
45 // MachineRegionInfo implementation
47 MachineRegionInfo::MachineRegionInfo() = default;
49 MachineRegionInfo::~MachineRegionInfo() = default;
51 void MachineRegionInfo::updateStatistics(MachineRegion *R) {
52 ++numMachineRegions;
54 // TODO: Slow. Should only be enabled if -stats is used.
55 if (R->isSimple())
56 ++numMachineSimpleRegions;
59 void MachineRegionInfo::recalculate(MachineFunction &F,
60 MachineDominatorTree *DT_,
61 MachinePostDominatorTree *PDT_,
62 MachineDominanceFrontier *DF_) {
63 DT = DT_;
64 PDT = PDT_;
65 DF = DF_;
67 MachineBasicBlock *Entry = GraphTraits<MachineFunction*>::getEntryNode(&F);
69 TopLevelRegion = new MachineRegion(Entry, nullptr, this, DT, nullptr);
70 updateStatistics(TopLevelRegion);
71 calculate(F);
74 //===----------------------------------------------------------------------===//
75 // MachineRegionInfoPass implementation
78 MachineRegionInfoPass::MachineRegionInfoPass() : MachineFunctionPass(ID) {
79 initializeMachineRegionInfoPassPass(*PassRegistry::getPassRegistry());
82 MachineRegionInfoPass::~MachineRegionInfoPass() = default;
84 bool MachineRegionInfoPass::runOnMachineFunction(MachineFunction &F) {
85 releaseMemory();
87 auto DT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
88 auto PDT =
89 &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
90 auto DF = &getAnalysis<MachineDominanceFrontier>();
92 RI.recalculate(F, DT, PDT, DF);
94 LLVM_DEBUG(RI.dump());
96 return false;
99 void MachineRegionInfoPass::releaseMemory() {
100 RI.releaseMemory();
103 void MachineRegionInfoPass::verifyAnalysis() const {
104 // Only do verification when user wants to, otherwise this expensive check
105 // will be invoked by PMDataManager::verifyPreservedAnalysis when
106 // a regionpass (marked PreservedAll) finish.
107 if (MachineRegionInfo::VerifyRegionInfo)
108 RI.verifyAnalysis();
111 void MachineRegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
112 AU.setPreservesAll();
113 AU.addRequired<MachineDominatorTreeWrapperPass>();
114 AU.addRequired<MachinePostDominatorTreeWrapperPass>();
115 AU.addRequired<MachineDominanceFrontier>();
116 MachineFunctionPass::getAnalysisUsage(AU);
119 void MachineRegionInfoPass::print(raw_ostream &OS, const Module *) const {
120 RI.print(OS);
123 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
124 LLVM_DUMP_METHOD void MachineRegionInfoPass::dump() const {
125 RI.dump();
127 #endif
129 char MachineRegionInfoPass::ID = 0;
130 char &MachineRegionInfoPassID = MachineRegionInfoPass::ID;
132 INITIALIZE_PASS_BEGIN(MachineRegionInfoPass, DEBUG_TYPE,
133 "Detect single entry single exit regions", true, true)
134 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
135 INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass)
136 INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier)
137 INITIALIZE_PASS_END(MachineRegionInfoPass, DEBUG_TYPE,
138 "Detect single entry single exit regions", true, true)
140 // Create methods available outside of this file, to use them
141 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
142 // the link time optimization.
144 namespace llvm {
146 FunctionPass *createMachineRegionInfoPass() {
147 return new MachineRegionInfoPass();
150 } // end namespace llvm