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
34using namespace llvm;
35using namespace MIPatternMatch;
36
37namespace {
38#define GET_GICOMBINER_TYPES
39#include "MipsGenPostLegalizeGICombiner.inc"
40#undef GET_GICOMBINER_TYPES
41
42class MipsPostLegalizerCombinerImpl : public Combiner {
43protected:
44 const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig;
45 const MipsSubtarget &STI;
46 const CombinerHelper Helper;
47
48public:
49 MipsPostLegalizerCombinerImpl(
50 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
51 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
60private:
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
70MipsPostLegalizerCombinerImpl::MipsPostLegalizerCombinerImpl(
71 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
72 GISelCSEInfo *CSEInfo,
73 const MipsPostLegalizerCombinerImplRuleConfig &RuleConfig,
74 const MipsSubtarget &STI, MachineDominatorTree *MDT,
75 const LegalizerInfo *LI)
76 : Combiner(MF, CInfo, &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
87class MipsPostLegalizerCombiner : public MachineFunctionPass {
88public:
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
99private:
100 bool IsOptNone;
101 MipsPostLegalizerCombinerImplRuleConfig RuleConfig;
102};
103} // end anonymous namespace
104
105void MipsPostLegalizerCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
106 AU.setPreservesCFG();
107 getSelectionDAGFallbackAnalysisUsage(AU);
108 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
109 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
110 if (!IsOptNone) {
111 AU.addRequired<MachineDominatorTreeWrapperPass>();
112 AU.addPreserved<MachineDominatorTreeWrapperPass>();
113 }
114 MachineFunctionPass::getAnalysisUsage(AU);
115}
116
117MipsPostLegalizerCombiner::MipsPostLegalizerCombiner(bool IsOptNone)
118 : MachineFunctionPass(ID), IsOptNone(IsOptNone) {
119 if (!RuleConfig.parseCommandLineOption())
120 report_fatal_error(reason: "Invalid rule identifier");
121}
122
123bool MipsPostLegalizerCombiner::runOnMachineFunction(MachineFunction &MF) {
124 if (MF.getProperties().hasFailedISel())
125 return false;
126 const Function &F = MF.getFunction();
127 bool EnableOpt =
128 MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);
129
130 const MipsSubtarget &ST = MF.getSubtarget<MipsSubtarget>();
131 const MipsLegalizerInfo *LI =
132 static_cast<const MipsLegalizerInfo *>(ST.getLegalizerInfo());
133
134 GISelValueTracking *VT =
135 &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
136 MachineDominatorTree *MDT =
137 IsOptNone ? nullptr
138 : &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
139 CombinerInfo CInfo(/*AllowIllegalOps*/ false, /*ShouldLegalizeIllegal*/ true,
140 LI, EnableOpt, F.hasOptSize(), F.hasMinSize());
141 MipsPostLegalizerCombinerImpl Impl(MF, CInfo, *VT, /*CSEInfo*/ nullptr,
142 RuleConfig, ST, MDT, LI);
143 return Impl.combineMachineInstrs();
144}
145
146char MipsPostLegalizerCombiner::ID = 0;
147INITIALIZE_PASS_BEGIN(MipsPostLegalizerCombiner, DEBUG_TYPE,
148 "Combine Mips machine instrs after legalization", false,
149 false)
150INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
151INITIALIZE_PASS_END(MipsPostLegalizerCombiner, DEBUG_TYPE,
152 "Combine Mips machine instrs after legalization", false,
153 false)
154
155FunctionPass *llvm::createMipsPostLegalizeCombiner(bool IsOptNone) {
156 return new MipsPostLegalizerCombiner(IsOptNone);
157}
158