Skip to content

Commit 6497283

Browse files
[RISCV][GISEL] Introduce the RISCVPostLegalizerLowering pass (#108991)
This is mostly a copy of the AArch64PostLegalizerLoweringPass, except it removes all of the AArch64 combines. This pass allows us to lower instructions after the generic post-legalization combiner has had a chance to run. We will be adding combines to this pass in future patches.
1 parent a729e70 commit 6497283

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed

llvm/lib/Target/RISCV/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ tablegen(LLVM RISCVGenPreLegalizeGICombiner.inc -gen-global-isel-combiner
2424
-combiners="RISCVPreLegalizerCombiner")
2525
tablegen(LLVM RISCVGenPostLegalizeGICombiner.inc -gen-global-isel-combiner
2626
-combiners="RISCVPostLegalizerCombiner")
27+
tablegen(LLVM RISCVGenPostLegalizeGILowering.inc -gen-global-isel-combiner
28+
-combiners="RISCVPostLegalizerLowering")
2729

2830
add_public_tablegen_target(RISCVCommonTableGen)
2931

@@ -63,6 +65,7 @@ add_llvm_target(RISCVCodeGen
6365
GISel/RISCVInstructionSelector.cpp
6466
GISel/RISCVLegalizerInfo.cpp
6567
GISel/RISCVPostLegalizerCombiner.cpp
68+
GISel/RISCVPostLegalizerLowering.cpp
6669
GISel/RISCVO0PreLegalizerCombiner.cpp
6770
GISel/RISCVPreLegalizerCombiner.cpp
6871
GISel/RISCVRegisterBankInfo.cpp
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//===--------------- RISCVPostLegalizerLowering.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 lowering for instructions.
11+
///
12+
/// This is used to offload pattern matching from the selector.
13+
///
14+
/// General optimization combines should be handled by either the
15+
/// RISCVPostLegalizerCombiner or the RISCVPreLegalizerCombiner.
16+
///
17+
//===----------------------------------------------------------------------===//
18+
19+
#include "RISCVSubtarget.h"
20+
21+
#include "llvm/CodeGen/GlobalISel/Combiner.h"
22+
#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
23+
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
24+
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
25+
#include "llvm/CodeGen/MachineFunctionPass.h"
26+
#include "llvm/CodeGen/TargetPassConfig.h"
27+
#include "llvm/InitializePasses.h"
28+
#include "llvm/Support/Debug.h"
29+
30+
#define GET_GICOMBINER_DEPS
31+
#include "RISCVGenPostLegalizeGILowering.inc"
32+
#undef GET_GICOMBINER_DEPS
33+
34+
#define DEBUG_TYPE "riscv-postlegalizer-lowering"
35+
36+
using namespace llvm;
37+
38+
namespace {
39+
40+
#define GET_GICOMBINER_TYPES
41+
#include "RISCVGenPostLegalizeGILowering.inc"
42+
#undef GET_GICOMBINER_TYPES
43+
44+
class RISCVPostLegalizerLoweringImpl : public Combiner {
45+
protected:
46+
// TODO: Make CombinerHelper methods const.
47+
mutable CombinerHelper Helper;
48+
const RISCVPostLegalizerLoweringImplRuleConfig &RuleConfig;
49+
const RISCVSubtarget &STI;
50+
51+
public:
52+
RISCVPostLegalizerLoweringImpl(
53+
MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
54+
GISelCSEInfo *CSEInfo,
55+
const RISCVPostLegalizerLoweringImplRuleConfig &RuleConfig,
56+
const RISCVSubtarget &STI);
57+
58+
static const char *getName() { return "RISCVPreLegalizerCombiner"; }
59+
60+
bool tryCombineAll(MachineInstr &I) const override;
61+
62+
private:
63+
#define GET_GICOMBINER_CLASS_MEMBERS
64+
#include "RISCVGenPostLegalizeGILowering.inc"
65+
#undef GET_GICOMBINER_CLASS_MEMBERS
66+
};
67+
68+
#define GET_GICOMBINER_IMPL
69+
#include "RISCVGenPostLegalizeGILowering.inc"
70+
#undef GET_GICOMBINER_IMPL
71+
72+
RISCVPostLegalizerLoweringImpl::RISCVPostLegalizerLoweringImpl(
73+
MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
74+
GISelCSEInfo *CSEInfo,
75+
const RISCVPostLegalizerLoweringImplRuleConfig &RuleConfig,
76+
const RISCVSubtarget &STI)
77+
: Combiner(MF, CInfo, TPC, /*KB*/ nullptr, CSEInfo),
78+
Helper(Observer, B, /*IsPreLegalize*/ true), RuleConfig(RuleConfig),
79+
STI(STI),
80+
#define GET_GICOMBINER_CONSTRUCTOR_INITS
81+
#include "RISCVGenPostLegalizeGILowering.inc"
82+
#undef GET_GICOMBINER_CONSTRUCTOR_INITS
83+
{
84+
}
85+
86+
class RISCVPostLegalizerLowering : public MachineFunctionPass {
87+
public:
88+
static char ID;
89+
90+
RISCVPostLegalizerLowering();
91+
92+
StringRef getPassName() const override {
93+
return "RISCVPostLegalizerLowering";
94+
}
95+
96+
bool runOnMachineFunction(MachineFunction &MF) override;
97+
void getAnalysisUsage(AnalysisUsage &AU) const override;
98+
99+
private:
100+
RISCVPostLegalizerLoweringImplRuleConfig RuleConfig;
101+
};
102+
} // end anonymous namespace
103+
104+
void RISCVPostLegalizerLowering::getAnalysisUsage(AnalysisUsage &AU) const {
105+
AU.addRequired<TargetPassConfig>();
106+
AU.setPreservesCFG();
107+
getSelectionDAGFallbackAnalysisUsage(AU);
108+
MachineFunctionPass::getAnalysisUsage(AU);
109+
}
110+
111+
RISCVPostLegalizerLowering::RISCVPostLegalizerLowering()
112+
: MachineFunctionPass(ID) {
113+
if (!RuleConfig.parseCommandLineOption())
114+
report_fatal_error("Invalid rule identifier");
115+
}
116+
117+
bool RISCVPostLegalizerLowering::runOnMachineFunction(MachineFunction &MF) {
118+
if (MF.getProperties().hasProperty(
119+
MachineFunctionProperties::Property::FailedISel))
120+
return false;
121+
assert(MF.getProperties().hasProperty(
122+
MachineFunctionProperties::Property::Legalized) &&
123+
"Expected a legalized function?");
124+
auto *TPC = &getAnalysis<TargetPassConfig>();
125+
const Function &F = MF.getFunction();
126+
127+
const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
128+
CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
129+
/*LegalizerInfo*/ nullptr, /*OptEnabled=*/true,
130+
F.hasOptSize(), F.hasMinSize());
131+
// Disable fixed-point iteration to reduce compile-time
132+
CInfo.MaxIterations = 1;
133+
CInfo.ObserverLvl = CombinerInfo::ObserverLevel::SinglePass;
134+
// PostLegalizerCombiner performs DCE, so a full DCE pass is unnecessary.
135+
CInfo.EnableFullDCE = false;
136+
RISCVPostLegalizerLoweringImpl Impl(MF, CInfo, TPC, /*CSEInfo*/ nullptr,
137+
RuleConfig, ST);
138+
return Impl.combineMachineInstrs();
139+
}
140+
141+
char RISCVPostLegalizerLowering::ID = 0;
142+
INITIALIZE_PASS_BEGIN(RISCVPostLegalizerLowering, DEBUG_TYPE,
143+
"Lower RISC-V MachineInstrs after legalization", false,
144+
false)
145+
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
146+
INITIALIZE_PASS_END(RISCVPostLegalizerLowering, DEBUG_TYPE,
147+
"Lower RISC-V MachineInstrs after legalization", false,
148+
false)
149+
150+
namespace llvm {
151+
FunctionPass *createRISCVPostLegalizerLowering() {
152+
return new RISCVPostLegalizerLowering();
153+
}
154+
} // end namespace llvm

llvm/lib/Target/RISCV/RISCV.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ void initializeRISCVO0PreLegalizerCombinerPass(PassRegistry &);
9999

100100
FunctionPass *createRISCVPreLegalizerCombiner();
101101
void initializeRISCVPreLegalizerCombinerPass(PassRegistry &);
102+
103+
FunctionPass *createRISCVPostLegalizerLowering();
104+
void initializeRISCVPostLegalizerLoweringPass(PassRegistry &);
102105
} // namespace llvm
103106

104107
#endif

llvm/lib/Target/RISCV/RISCVCombine.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ def RISCVO0PreLegalizerCombiner: GICombiner<
1919
"RISCVO0PreLegalizerCombinerImpl", [optnone_combines]> {
2020
}
2121

22+
// Post-legalization combines which should happen at all optimization levels.
23+
// (E.g. ones that facilitate matching for the selector) For example, matching
24+
// pseudos.
25+
def RISCVPostLegalizerLowering
26+
: GICombiner<"RISCVPostLegalizerLoweringImpl", []> {
27+
}
28+
2229
// Post-legalization combines which are primarily optimizations.
2330
// TODO: Add more combines.
2431
def RISCVPostLegalizerCombiner

llvm/lib/Target/RISCV/RISCVTargetMachine.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
111111
initializeRISCVO0PreLegalizerCombinerPass(*PR);
112112
initializeRISCVPreLegalizerCombinerPass(*PR);
113113
initializeRISCVPostLegalizerCombinerPass(*PR);
114+
initializeRISCVPostLegalizerLoweringPass(*PR);
114115
initializeKCFIPass(*PR);
115116
initializeRISCVDeadRegisterDefinitionsPass(*PR);
116117
initializeRISCVMakeCompressibleOptPass(*PR);
@@ -482,6 +483,7 @@ bool RISCVPassConfig::addLegalizeMachineIR() {
482483
void RISCVPassConfig::addPreRegBankSelect() {
483484
if (getOptLevel() != CodeGenOptLevel::None)
484485
addPass(createRISCVPostLegalizerCombiner());
486+
addPass(createRISCVPostLegalizerLowering());
485487
}
486488

487489
bool RISCVPassConfig::addRegBankSelect() {

llvm/test/CodeGen/RISCV/GlobalISel/gisel-commandline-option.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
; ENABLED-NEXT: Legalizer
2424
; ENABLED-O1-NEXT: MachineDominator Tree Construction
2525
; ENABLED-O1-NEXT: RISCVPostLegalizerCombiner
26+
; ENABLED-NEXT: RISCVPostLegalizerLowering
2627
; ENABLED-NEXT: RegBankSelect
2728
; ENABLED-NEXT: Analysis for ComputingKnownBits
2829
; ENABLED-O1-NEXT: Lazy Branch Probability Analysis

0 commit comments

Comments
 (0)