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/GISelValueTracking.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 GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
52 const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig,
53 const MipsSubtarget &STI, MachineDominatorTree *MDT,
54 const LegalizerInfo *LI);
55
getName()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
MipsPostLegalizerCombinerImpl(MachineFunction & MF,CombinerInfo & CInfo,const TargetPassConfig * TPC,GISelValueTracking & VT,GISelCSEInfo * CSEInfo,const MipsPostLegalizerCombinerImplRuleConfig & RuleConfig,const MipsSubtarget & STI,MachineDominatorTree * MDT,const LegalizerInfo * LI)70 MipsPostLegalizerCombinerImpl::MipsPostLegalizerCombinerImpl(
71 MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
72 GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
73 const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig,
74 const MipsSubtarget &STI, MachineDominatorTree *MDT,
75 const LegalizerInfo *LI)
76 : Combiner(MF, CInfo, TPC, &VT, CSEInfo), RuleConfig(RuleConfig), STI(STI),
77 Helper(Observer, B, /*IsPreLegalize*/ false, &VT, 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
getPassName() const93 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
getAnalysisUsage(AnalysisUsage & AU) const105 void MipsPostLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
106 AU.addRequired<TargetPassConfig>();
107 AU.setPreservesCFG();
108 getSelectionDAGFallbackAnalysisUsage(AU);
109 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
110 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
111 if (!IsOptNone) {
112 AU.addRequired<MachineDominatorTreeWrapperPass>();
113 AU.addPreserved<MachineDominatorTreeWrapperPass>();
114 }
115 MachineFunctionPass::getAnalysisUsage(AU);
116 }
117
MipsPostLegalizerCombiner(bool IsOptNone)118 MipsPostLegalizerCombiner::MipsPostLegalizerCombiner(bool IsOptNone)
119 : MachineFunctionPass(ID), IsOptNone(IsOptNone) {
120 if (!RuleConfig.parseCommandLineOption())
121 report_fatal_error("Invalid rule identifier");
122 }
123
runOnMachineFunction(MachineFunction & MF)124 bool MipsPostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {
125 if (MF.getProperties().hasFailedISel())
126 return false;
127 auto *TPC = &getAnalysis<TargetPassConfig>();
128 const Function &F = MF.getFunction();
129 bool EnableOpt =
130 MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);
131
132 const MipsSubtarget &ST = MF.getSubtarget<MipsSubtarget>();
133 const MipsLegalizerInfo *LI =
134 static_cast<const MipsLegalizerInfo *>(ST.getLegalizerInfo());
135
136 GISelValueTracking *VT =
137 &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
138 MachineDominatorTree *MDT =
139 IsOptNone ? nullptr
140 : &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
141 CombinerInfo CInfo(/*AllowIllegalOps*/ false, /*ShouldLegalizeIllegal*/ true,
142 LI, EnableOpt, F.hasOptSize(), F.hasMinSize());
143 MipsPostLegalizerCombinerImpl Impl(MF, CInfo, TPC, *VT, /*CSEInfo*/ nullptr,
144 RuleConfig, ST, MDT, LI);
145 return Impl.combineMachineInstrs();
146 }
147
148 char MipsPostLegalizerCombiner::ID = 0;
149 INITIALIZE_PASS_BEGIN(MipsPostLegalizerCombiner, DEBUG_TYPE,
150 "Combine Mips machine instrs after legalization", false,
151 false)
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)152 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
153 INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
154 INITIALIZE_PASS_END(MipsPostLegalizerCombiner, DEBUG_TYPE,
155 "Combine Mips machine instrs after legalization", false,
156 false)
157
158 FunctionPass *llvm::createMipsPostLegalizeCombiner(bool IsOptNone) {
159 return new MipsPostLegalizerCombiner(IsOptNone);
160 }
161