xref: /llvm-project/llvm/lib/Target/Mips/MipsPostLegalizerCombiner.cpp (revision ee7ca0dddafb609090ad1789570c099d95c0afb6)
1 //=== lib/CodeGen/GlobalISel/MipsPostLegalizerCombiner.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 //===----------------------------------------------------------------------===//
8 //
9 // This pass does combining of machine instructions at the generic MI level,
10 // after the legalizer.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "MCTargetDesc/MipsMCTargetDesc.h"
15 #include "Mips.h"
16 #include "MipsLegalizerInfo.h"
17 #include "MipsSubtarget.h"
18 #include "llvm/CodeGen/GlobalISel/Combiner.h"
19 #include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
20 #include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
21 #include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
22 #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
23 #include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
24 #include "llvm/CodeGen/MachineDominators.h"
25 #include "llvm/CodeGen/TargetPassConfig.h"
26 #include "llvm/Target/TargetMachine.h"
27 
28 #define GET_GICOMBINER_DEPS
29 #include "MipsGenPostLegalizeGICombiner.inc"
30 #undef GET_GICOMBINER_DEPS
31 
32 #define DEBUG_TYPE "mips-postlegalizer-combiner"
33 
34 using namespace llvm;
35 using namespace MIPatternMatch;
36 
37 namespace {
38 #define GET_GICOMBINER_TYPES
39 #include "MipsGenPostLegalizeGICombiner.inc"
40 #undef GET_GICOMBINER_TYPES
41 
42 class MipsPostLegalizerCombinerImpl : public Combiner {
43 protected:
44   const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig;
45   const MipsSubtarget &STI;
46   const CombinerHelper Helper;
47 
48 public:
49   MipsPostLegalizerCombinerImpl(
50       MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
51       GISelKnownBits &KB, GISelCSEInfo *CSEInfo,
52       const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig,
53       const MipsSubtarget &STI, MachineDominatorTree *MDT,
54       const LegalizerInfo *LI);
55 
56   static const char *getName() { return "MipsPostLegalizerCombiner"; }
57 
58   bool tryCombineAll(MachineInstr &I) const override;
59 
60 private:
61 #define GET_GICOMBINER_CLASS_MEMBERS
62 #include "MipsGenPostLegalizeGICombiner.inc"
63 #undef GET_GICOMBINER_CLASS_MEMBERS
64 };
65 
66 #define GET_GICOMBINER_IMPL
67 #include "MipsGenPostLegalizeGICombiner.inc"
68 #undef GET_GICOMBINER_IMPL
69 
70 MipsPostLegalizerCombinerImpl::MipsPostLegalizerCombinerImpl(
71     MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
72     GISelKnownBits &KB, GISelCSEInfo *CSEInfo,
73     const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig,
74     const MipsSubtarget &STI, MachineDominatorTree *MDT,
75     const LegalizerInfo *LI)
76     : Combiner(MF, CInfo, TPC, &KB, CSEInfo), RuleConfig(RuleConfig), STI(STI),
77       Helper(Observer, B, /*IsPreLegalize*/ false, &KB, MDT, LI),
78 #define GET_GICOMBINER_CONSTRUCTOR_INITS
79 #include "MipsGenPostLegalizeGICombiner.inc"
80 #undef GET_GICOMBINER_CONSTRUCTOR_INITS
81 {
82 }
83 
84 // Pass boilerplate
85 // ================
86 
87 class MipsPostLegalizerCombiner : public MachineFunctionPass {
88 public:
89   static char ID;
90 
91   MipsPostLegalizerCombiner(bool IsOptNone = false);
92 
93   StringRef getPassName() const override { return "MipsPostLegalizerCombiner"; }
94 
95   bool runOnMachineFunction(MachineFunction &MF) override;
96 
97   void getAnalysisUsage(AnalysisUsage &AU) const override;
98 
99 private:
100   bool IsOptNone;
101   MipsPostLegalizerCombinerImplRuleConfig RuleConfig;
102 };
103 } // end anonymous namespace
104 
105 void MipsPostLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
106   AU.addRequired<TargetPassConfig>();
107   AU.setPreservesCFG();
108   getSelectionDAGFallbackAnalysisUsage(AU);
109   AU.addRequired<GISelKnownBitsAnalysis>();
110   AU.addPreserved<GISelKnownBitsAnalysis>();
111   if (!IsOptNone) {
112     AU.addRequired<MachineDominatorTreeWrapperPass>();
113     AU.addPreserved<MachineDominatorTreeWrapperPass>();
114   }
115   MachineFunctionPass::getAnalysisUsage(AU);
116 }
117 
118 MipsPostLegalizerCombiner::MipsPostLegalizerCombiner(bool IsOptNone)
119     : MachineFunctionPass(ID), IsOptNone(IsOptNone) {
120   initializeMipsPostLegalizerCombinerPass(*PassRegistry::getPassRegistry());
121 
122   if (!RuleConfig.parseCommandLineOption())
123     report_fatal_error("Invalid rule identifier");
124 }
125 
126 bool MipsPostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {
127   if (MF.getProperties().hasProperty(
128           MachineFunctionProperties::Property::FailedISel))
129     return false;
130   auto *TPC = &getAnalysis<TargetPassConfig>();
131   const Function &F = MF.getFunction();
132   bool EnableOpt =
133       MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);
134 
135   const MipsSubtarget &ST = MF.getSubtarget<MipsSubtarget>();
136   const MipsLegalizerInfo *LI =
137       static_cast<const MipsLegalizerInfo *>(ST.getLegalizerInfo());
138 
139   GISelKnownBits *KB = &getAnalysis<GISelKnownBitsAnalysis>().get(MF);
140   MachineDominatorTree *MDT =
141       IsOptNone ? nullptr
142                 : &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
143   CombinerInfo CInfo(/*AllowIllegalOps*/ false, /*ShouldLegalizeIllegal*/ true,
144                      LI, EnableOpt, F.hasOptSize(), F.hasMinSize());
145   MipsPostLegalizerCombinerImpl Impl(MF, CInfo, TPC, *KB, /*CSEInfo*/ nullptr,
146                                      RuleConfig, ST, MDT, LI);
147   return Impl.combineMachineInstrs();
148 }
149 
150 char MipsPostLegalizerCombiner::ID = 0;
151 INITIALIZE_PASS_BEGIN(MipsPostLegalizerCombiner, DEBUG_TYPE,
152                       "Combine Mips machine instrs after legalization", false,
153                       false)
154 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
155 INITIALIZE_PASS_DEPENDENCY(GISelKnownBitsAnalysis)
156 INITIALIZE_PASS_END(MipsPostLegalizerCombiner, DEBUG_TYPE,
157                     "Combine Mips machine instrs after legalization", false,
158                     false)
159 
160 namespace llvm {
161 FunctionPass *createMipsPostLegalizeCombiner(bool IsOptNone) {
162   return new MipsPostLegalizerCombiner(IsOptNone);
163 }
164 } // end namespace llvm
165