1//===--------------- X86PostLegalizerCombiner.cpp ---------------*- C++ -*-===//
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/// \file
10/// Post-legalization combines on generic MachineInstrs.
11///
12/// The combines here must preserve instruction legality.
13///
14/// Lowering combines (e.g. pseudo matching) should be handled by
15/// X86PostLegalizerLowering.
16///
17/// Combines which don't rely on instruction legality should go in the
18/// X86PreLegalizerCombiner.
19///
20//===----------------------------------------------------------------------===//
21#include "X86.h"
22#include "X86TargetMachine.h"
23#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
24#include "llvm/CodeGen/GlobalISel/Combiner.h"
25#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
26#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
27#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
28#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
29#include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"
30#include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
31#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
32#include "llvm/CodeGen/GlobalISel/Utils.h"
33#include "llvm/CodeGen/MachineDominators.h"
34#include "llvm/CodeGen/MachineFunctionPass.h"
35#include "llvm/CodeGen/TargetPassConfig.h"
36
37#define GET_GICOMBINER_DEPS
38#include "X86GenPostLegalizeGICombiner.inc"
39#undef GET_GICOMBINER_DEPS
40
41#define DEBUG_TYPE "X86-postlegalizer-combiner"
42
43using namespace llvm;
44using namespace MIPatternMatch;
45
46namespace {
47
48CombinerInfo createCombinerInfo(bool OptEnabled, const Function &F) {
49 CombinerInfo CInfo(/*AllowIllegalOps=*/true,
50 /*ShouldLegalizeIllegal=*/false,
51 /*LInfo=*/nullptr, /*OptEnabled=*/OptEnabled,
52 /*OptSize=*/F.hasOptSize(), /*MinSize=*/F.hasMinSize());
53 // Disable fixed-point iteration to reduce compile-time
54 CInfo.MaxIterations = 1;
55 CInfo.ObserverLvl = CombinerInfo::ObserverLevel::SinglePass;
56 // Legalizer performs DCE, so a full DCE pass is unnecessary.
57 CInfo.EnableFullDCE = false;
58 return CInfo;
59}
60
61#define GET_GICOMBINER_TYPES
62#include "X86GenPostLegalizeGICombiner.inc"
63#undef GET_GICOMBINER_TYPES
64
65class X86PostLegalizerCombinerImpl : public Combiner {
66protected:
67 const CombinerHelper Helper;
68 const X86PostLegalizerCombinerImplRuleConfig &RuleConfig;
69 const X86Subtarget &STI;
70
71public:
72 X86PostLegalizerCombinerImpl(
73 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
74 GISelCSEInfo *CSEInfo,
75 const X86PostLegalizerCombinerImplRuleConfig &RuleConfig,
76 MachineDominatorTree *MDT);
77
78 static const char *getName() { return "X86PostLegalizerCombiner"; }
79
80 bool tryCombineAll(MachineInstr &I) const override;
81 bool tryCombineAllImpl(MachineInstr &I) const;
82
83private:
84#define GET_GICOMBINER_CLASS_MEMBERS
85#include "X86GenPostLegalizeGICombiner.inc"
86#undef GET_GICOMBINER_CLASS_MEMBERS
87};
88
89#define GET_GICOMBINER_IMPL
90#include "X86GenPostLegalizeGICombiner.inc"
91#undef GET_GICOMBINER_IMPL
92
93X86PostLegalizerCombinerImpl::X86PostLegalizerCombinerImpl(
94 MachineFunction &MF, CombinerInfo &CInfo, GISelValueTracking &VT,
95 GISelCSEInfo *CSEInfo,
96 const X86PostLegalizerCombinerImplRuleConfig &RuleConfig,
97 MachineDominatorTree *MDT)
98 : Combiner(MF, CInfo, &VT, CSEInfo),
99 Helper(Observer, B, /*IsPreLegalize=*/false, &VT, MDT,
100 MF.getSubtarget<X86Subtarget>().getLegalizerInfo()),
101 RuleConfig(RuleConfig), STI(MF.getSubtarget<X86Subtarget>()),
102#define GET_GICOMBINER_CONSTRUCTOR_INITS
103#include "X86GenPostLegalizeGICombiner.inc"
104#undef GET_GICOMBINER_CONSTRUCTOR_INITS
105{
106}
107
108bool X86PostLegalizerCombinerImpl::tryCombineAll(MachineInstr &MI) const {
109 return tryCombineAllImpl(I&: MI);
110}
111
112class X86PostLegalizerCombinerLegacy : public MachineFunctionPass {
113public:
114 static char ID;
115
116 X86PostLegalizerCombinerLegacy();
117
118 StringRef getPassName() const override {
119 return "X86PostLegalizerCombinerLegacy";
120 }
121
122 bool runOnMachineFunction(MachineFunction &MF) override;
123 void getAnalysisUsage(AnalysisUsage &AU) const override;
124
125private:
126 X86PostLegalizerCombinerImplRuleConfig RuleConfig;
127};
128} // end anonymous namespace
129
130void X86PostLegalizerCombinerLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
131 AU.addRequired<TargetPassConfig>();
132 AU.setPreservesCFG();
133 getSelectionDAGFallbackAnalysisUsage(AU);
134 AU.addRequired<GISelValueTrackingAnalysisLegacy>();
135 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
136 // This is only added when processing level is not OptNone.
137 AU.addRequired<MachineDominatorTreeWrapperPass>();
138 AU.addRequired<GISelCSEAnalysisWrapperPass>();
139 AU.addPreserved<GISelCSEAnalysisWrapperPass>();
140
141 MachineFunctionPass::getAnalysisUsage(AU);
142}
143
144X86PostLegalizerCombinerLegacy::X86PostLegalizerCombinerLegacy()
145 : MachineFunctionPass(ID) {
146 if (!RuleConfig.parseCommandLineOption())
147 reportFatalInternalError(reason: "Invalid rule identifier");
148}
149
150bool X86PostLegalizerCombinerLegacy::runOnMachineFunction(MachineFunction &MF) {
151 if (MF.getProperties().hasFailedISel())
152 return false;
153 assert(MF.getProperties().hasLegalized() && "Expected a legalized function?");
154 auto *TPC = &getAnalysis<TargetPassConfig>();
155 const Function &F = MF.getFunction();
156
157 GISelValueTracking *VT =
158 &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
159 MachineDominatorTree *MDT =
160 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
161 GISelCSEAnalysisWrapper &Wrapper =
162 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
163 auto *CSEInfo = &Wrapper.get(CSEOpt: TPC->getCSEConfig());
164
165 CombinerInfo CInfo = createCombinerInfo(OptEnabled: !skipFunction(F), F);
166
167 X86PostLegalizerCombinerImpl Impl(MF, CInfo, *VT, CSEInfo, RuleConfig, MDT);
168 return Impl.combineMachineInstrs();
169}
170
171char X86PostLegalizerCombinerLegacy::ID = 0;
172INITIALIZE_PASS_BEGIN(X86PostLegalizerCombinerLegacy, DEBUG_TYPE,
173 "Combine X86 MachineInstrs after legalization", false,
174 false)
175INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
176INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
177INITIALIZE_PASS_END(X86PostLegalizerCombinerLegacy, DEBUG_TYPE,
178 "Combine X86 MachineInstrs after legalization", false,
179 false)
180
181namespace llvm {
182
183PreservedAnalyses
184X86PostLegalizerCombinerPass::run(MachineFunction &MF,
185 MachineFunctionAnalysisManager &MFAM) {
186 if (MF.getProperties().hasFailedISel())
187 return PreservedAnalyses::all();
188 assert(MF.getProperties().hasLegalized() && "Expected a legalized function.");
189 const Function &F = MF.getFunction();
190
191 GISelValueTracking VT = MFAM.getResult<GISelValueTrackingAnalysis>(IR&: MF);
192 MachineDominatorTree &MDT = MFAM.getResult<MachineDominatorTreeAnalysis>(IR&: MF);
193 auto &CSEInfo = MFAM.getResult<GISelCSEAnalysis>(IR&: MF);
194
195 CombinerInfo CInfo = createCombinerInfo(OptEnabled: true, F);
196
197 X86PostLegalizerCombinerImplRuleConfig RuleConfig;
198 if (!RuleConfig.parseCommandLineOption())
199 reportFatalInternalError(reason: "Invalid rule identifier");
200
201 X86PostLegalizerCombinerImpl Impl(MF, CInfo, VT, CSEInfo.get(), RuleConfig,
202 &MDT);
203 if (!Impl.combineMachineInstrs())
204 return PreservedAnalyses::all();
205
206 PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses();
207 PA.preserveSet<CFGAnalyses>();
208 PA.preserve<GISelCSEAnalysis>();
209 PA.preserve<GISelValueTrackingAnalysis>();
210 return PA;
211}
212
213FunctionPass *createX86PostLegalizerCombinerLegacy() {
214 return new X86PostLegalizerCombinerLegacy();
215}
216} // end namespace llvm
217