[llvm] [WebAssembly][GlobalISel] Part 1 - Setup skeleton (PR #178796)
Demetrius Kanios via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 2 11:57:20 PST 2026
https://github.com/QuantumSegfault updated https://github.com/llvm/llvm-project/pull/178796
>From 1e512191ecfc8b6b60991c2445f2de03bbad28a9 Mon Sep 17 00:00:00 2001
From: Demetrius Kanios <demetrius at kanios.net>
Date: Thu, 29 Jan 2026 16:23:11 -0800
Subject: [PATCH 1/3] Setup skeleton for WASM GlobalISel
---
llvm/lib/Target/WebAssembly/CMakeLists.txt | 18 ++
.../GISel/WebAssemblyCallLowering.cpp | 65 +++++++
.../GISel/WebAssemblyCallLowering.h | 43 +++++
.../GISel/WebAssemblyInstructionSelector.cpp | 100 ++++++++++
.../GISel/WebAssemblyLegalizerInfo.cpp | 23 +++
.../GISel/WebAssemblyLegalizerInfo.h | 29 +++
.../WebAssemblyO0PreLegalizerCombiner.cpp | 157 +++++++++++++++
.../WebAssemblyPostLegalizerCombiner.cpp | 182 ++++++++++++++++++
.../GISel/WebAssemblyPreLegalizerCombiner.cpp | 176 +++++++++++++++++
.../GISel/WebAssemblyRegisterBankInfo.cpp | 33 ++++
.../GISel/WebAssemblyRegisterBankInfo.h | 40 ++++
llvm/lib/Target/WebAssembly/WebAssembly.h | 18 ++
llvm/lib/Target/WebAssembly/WebAssembly.td | 1 +
.../Target/WebAssembly/WebAssemblyCombine.td | 26 +++
.../Target/WebAssembly/WebAssemblyGISel.td | 17 ++
.../WebAssembly/WebAssemblyRegisterBanks.td | 22 +++
.../WebAssembly/WebAssemblySubtarget.cpp | 31 ++-
.../Target/WebAssembly/WebAssemblySubtarget.h | 14 ++
.../WebAssembly/WebAssemblyTargetMachine.cpp | 56 ++++++
.../GlobalISel/gisel-commandline-option.ll | 53 +++++
20 files changed, 1103 insertions(+), 1 deletion(-)
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.cpp
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.h
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.cpp
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.h
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyPreLegalizerCombiner.cpp
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.cpp
create mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.h
create mode 100644 llvm/lib/Target/WebAssembly/WebAssemblyCombine.td
create mode 100644 llvm/lib/Target/WebAssembly/WebAssemblyGISel.td
create mode 100644 llvm/lib/Target/WebAssembly/WebAssemblyRegisterBanks.td
create mode 100644 llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll
diff --git a/llvm/lib/Target/WebAssembly/CMakeLists.txt b/llvm/lib/Target/WebAssembly/CMakeLists.txt
index 17df119d62709..217df5f987e85 100644
--- a/llvm/lib/Target/WebAssembly/CMakeLists.txt
+++ b/llvm/lib/Target/WebAssembly/CMakeLists.txt
@@ -9,13 +9,30 @@ tablegen(LLVM WebAssemblyGenDisassemblerTables.inc -gen-disassembler)
tablegen(LLVM WebAssemblyGenFastISel.inc -gen-fast-isel)
tablegen(LLVM WebAssemblyGenInstrInfo.inc -gen-instr-info)
tablegen(LLVM WebAssemblyGenMCCodeEmitter.inc -gen-emitter)
+tablegen(LLVM WebAssemblyGenRegisterBank.inc -gen-register-bank)
tablegen(LLVM WebAssemblyGenRegisterInfo.inc -gen-register-info)
tablegen(LLVM WebAssemblyGenSDNodeInfo.inc -gen-sd-node-info)
tablegen(LLVM WebAssemblyGenSubtargetInfo.inc -gen-subtarget)
+set(LLVM_TARGET_DEFINITIONS WebAssemblyGISel.td)
+tablegen(LLVM WebAssemblyGenGlobalISel.inc -gen-global-isel)
+tablegen(LLVM WebAssemblyGenO0PreLegalizeGICombiner.inc -gen-global-isel-combiner
+ -combiners="WebAssemblyO0PreLegalizerCombiner")
+tablegen(LLVM WebAssemblyGenPreLegalizeGICombiner.inc -gen-global-isel-combiner
+ -combiners="WebAssemblyPreLegalizerCombiner")
+tablegen(LLVM WebAssemblyGenPostLegalizeGICombiner.inc -gen-global-isel-combiner
+ -combiners="WebAssemblyPostLegalizerCombiner")
+
add_public_tablegen_target(WebAssemblyCommonTableGen)
add_llvm_target(WebAssemblyCodeGen
+ GISel/WebAssemblyCallLowering.cpp
+ GISel/WebAssemblyInstructionSelector.cpp
+ GISel/WebAssemblyO0PreLegalizerCombiner.cpp
+ GISel/WebAssemblyPostLegalizerCombiner.cpp
+ GISel/WebAssemblyPreLegalizerCombiner.cpp
+ GISel/WebAssemblyLegalizerInfo.cpp
+ GISel/WebAssemblyRegisterBankInfo.cpp
WebAssemblyAddMissingPrototypes.cpp
WebAssemblyArgumentMove.cpp
WebAssemblyAsmPrinter.cpp
@@ -72,6 +89,7 @@ add_llvm_target(WebAssemblyCodeGen
CodeGen
CodeGenTypes
Core
+ GlobalISel
MC
Scalar
SelectionDAG
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.cpp
new file mode 100644
index 0000000000000..3d0bbcc9a1c7b
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.cpp
@@ -0,0 +1,65 @@
+//===-- WebAssemblyCallLowering.cpp - Call lowering for GlobalISel -*- C++ -*-//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file implements the lowering of LLVM calls to machine code calls for
+/// GlobalISel.
+///
+//===----------------------------------------------------------------------===//
+
+#include "WebAssemblyCallLowering.h"
+#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
+#include "WebAssemblyISelLowering.h"
+#include "WebAssemblyMachineFunctionInfo.h"
+#include "WebAssemblySubtarget.h"
+#include "WebAssemblyUtilities.h"
+#include "llvm/CodeGen/Analysis.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DebugLoc.h"
+#include "llvm/IR/Value.h"
+
+#define DEBUG_TYPE "wasm-call-lowering"
+
+using namespace llvm;
+
+WebAssemblyCallLowering::WebAssemblyCallLowering(
+ const WebAssemblyTargetLowering &TLI)
+ : CallLowering(&TLI) {}
+
+bool WebAssemblyCallLowering::canLowerReturn(MachineFunction &MF,
+ CallingConv::ID CallConv,
+ SmallVectorImpl<BaseArgInfo> &Outs,
+ bool IsVarArg) const {
+ return WebAssembly::canLowerReturn(Outs.size(),
+ &MF.getSubtarget<WebAssemblySubtarget>());
+}
+
+bool WebAssemblyCallLowering::lowerReturn(MachineIRBuilder &MIRBuilder,
+ const Value *Val,
+ ArrayRef<Register> VRegs,
+ FunctionLoweringInfo &FLI,
+ Register SwiftErrorVReg) const {
+ if (!Val)
+ return true; // allow only void returns for now
+
+ return false;
+}
+
+bool WebAssemblyCallLowering::lowerFormalArguments(
+ MachineIRBuilder &MIRBuilder, const Function &F,
+ ArrayRef<ArrayRef<Register>> VRegs, FunctionLoweringInfo &FLI) const {
+ if (VRegs.empty())
+ return true; // allow only empty signatures for now
+
+ return false;
+}
+
+bool WebAssemblyCallLowering::lowerCall(MachineIRBuilder &MIRBuilder,
+ CallLoweringInfo &Info) const {
+ return false;
+}
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.h b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.h
new file mode 100644
index 0000000000000..d22f7cbd17eb3
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyCallLowering.h
@@ -0,0 +1,43 @@
+//===-- WebAssemblyCallLowering.h - Call lowering for GlobalISel -*- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file describes how to lower LLVM calls to machine code calls.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_GISEL_WEBASSEMBLYCALLLOWERING_H
+#define LLVM_LIB_TARGET_WEBASSEMBLY_GISEL_WEBASSEMBLYCALLLOWERING_H
+
+#include "WebAssemblyISelLowering.h"
+#include "llvm/CodeGen/GlobalISel/CallLowering.h"
+#include "llvm/IR/CallingConv.h"
+
+namespace llvm {
+
+class WebAssemblyTargetLowering;
+
+class WebAssemblyCallLowering : public CallLowering {
+public:
+ WebAssemblyCallLowering(const WebAssemblyTargetLowering &TLI);
+
+ bool canLowerReturn(MachineFunction &MF, CallingConv::ID CallConv,
+ SmallVectorImpl<BaseArgInfo> &Outs,
+ bool IsVarArg) const override;
+ bool lowerReturn(MachineIRBuilder &MIRBuilder, const Value *Val,
+ ArrayRef<Register> VRegs, FunctionLoweringInfo &FLI,
+ Register SwiftErrorVReg) const override;
+ bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F,
+ ArrayRef<ArrayRef<Register>> VRegs,
+ FunctionLoweringInfo &FLI) const override;
+ bool lowerCall(MachineIRBuilder &MIRBuilder,
+ CallLoweringInfo &Info) const override;
+};
+} // namespace llvm
+
+#endif
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp
new file mode 100644
index 0000000000000..97caaa1d504ae
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp
@@ -0,0 +1,100 @@
+//===- WebAssemblyInstructionSelector.cpp ------------------------*- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// This file implements the targeting of the InstructionSelector class for
+/// WebAssembly.
+/// \todo This should be generated by TableGen.
+//===----------------------------------------------------------------------===//
+
+#include "GISel/WebAssemblyRegisterBankInfo.h"
+#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
+#include "WebAssemblyRegisterInfo.h"
+#include "WebAssemblySubtarget.h"
+#include "WebAssemblyTargetMachine.h"
+#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
+#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
+#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
+#include "llvm/CodeGen/GlobalISel/Utils.h"
+#include "llvm/CodeGen/MachineOperand.h"
+#include "llvm/CodeGen/TargetLowering.h"
+#include "llvm/IR/IntrinsicsWebAssembly.h"
+
+#define DEBUG_TYPE "wasm-isel"
+
+using namespace llvm;
+
+namespace {
+
+#define GET_GLOBALISEL_PREDICATE_BITSET
+#include "WebAssemblyGenGlobalISel.inc"
+#undef GET_GLOBALISEL_PREDICATE_BITSET
+
+class WebAssemblyInstructionSelector : public InstructionSelector {
+public:
+ WebAssemblyInstructionSelector(const WebAssemblyTargetMachine &TM,
+ const WebAssemblySubtarget &STI,
+ const WebAssemblyRegisterBankInfo &RBI);
+
+ bool select(MachineInstr &I) override;
+
+ static const char *getName() { return DEBUG_TYPE; }
+
+private:
+ bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
+
+ const WebAssemblyTargetMachine &TM;
+ const WebAssemblySubtarget &STI;
+ const WebAssemblyInstrInfo &TII;
+ const WebAssemblyRegisterInfo &TRI;
+ const WebAssemblyRegisterBankInfo &RBI;
+
+#define GET_GLOBALISEL_PREDICATES_DECL
+#include "WebAssemblyGenGlobalISel.inc"
+#undef GET_GLOBALISEL_PREDICATES_DECL
+
+#define GET_GLOBALISEL_TEMPORARIES_DECL
+#include "WebAssemblyGenGlobalISel.inc"
+#undef GET_GLOBALISEL_TEMPORARIES_DECL
+};
+
+} // end anonymous namespace
+
+#define GET_GLOBALISEL_IMPL
+#include "WebAssemblyGenGlobalISel.inc"
+#undef GET_GLOBALISEL_IMPL
+
+WebAssemblyInstructionSelector::WebAssemblyInstructionSelector(
+ const WebAssemblyTargetMachine &TM, const WebAssemblySubtarget &STI,
+ const WebAssemblyRegisterBankInfo &RBI)
+ : TM(TM), STI(STI), TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()),
+ RBI(RBI),
+
+#define GET_GLOBALISEL_PREDICATES_INIT
+#include "WebAssemblyGenGlobalISel.inc"
+#undef GET_GLOBALISEL_PREDICATES_INIT
+#define GET_GLOBALISEL_TEMPORARIES_INIT
+#include "WebAssemblyGenGlobalISel.inc"
+#undef GET_GLOBALISEL_TEMPORARIES_INIT
+{
+}
+
+bool WebAssemblyInstructionSelector::select(MachineInstr &I) {
+ if (selectImpl(I, *CoverageInfo))
+ return true;
+
+ return false;
+}
+
+namespace llvm {
+InstructionSelector *
+createWebAssemblyInstructionSelector(const WebAssemblyTargetMachine &TM,
+ const WebAssemblySubtarget &Subtarget,
+ const WebAssemblyRegisterBankInfo &RBI) {
+ return new WebAssemblyInstructionSelector(TM, Subtarget, RBI);
+}
+} // namespace llvm
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.cpp
new file mode 100644
index 0000000000000..10b1287eba070
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.cpp
@@ -0,0 +1,23 @@
+//===- WebAssemblyLegalizerInfo.cpp ------------------------------*- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// This file implements the targeting of the Machinelegalizer class for
+/// WebAssembly
+//===----------------------------------------------------------------------===//
+
+#include "WebAssemblyLegalizerInfo.h"
+
+#define DEBUG_TYPE "wasm-legalinfo"
+
+using namespace llvm;
+using namespace LegalizeActions;
+
+WebAssemblyLegalizerInfo::WebAssemblyLegalizerInfo(
+ const WebAssemblySubtarget &ST) {
+ getLegacyLegalizerInfo().computeTables();
+}
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.h b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.h
new file mode 100644
index 0000000000000..c02205fc7ae0d
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyLegalizerInfo.h
@@ -0,0 +1,29 @@
+//===- WebAssemblyLegalizerInfo.h --------------------------------*- C++ -*-==//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// This file declares the targeting of the Machinelegalizer class for
+/// WebAssembly
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_GISEL_WEBASSEMBLYMACHINELEGALIZER_H
+#define LLVM_LIB_TARGET_WEBASSEMBLY_GISEL_WEBASSEMBLYMACHINELEGALIZER_H
+
+#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
+
+namespace llvm {
+
+class WebAssemblySubtarget;
+
+/// This class provides the information for the BPF target legalizer for
+/// GlobalISel.
+class WebAssemblyLegalizerInfo : public LegalizerInfo {
+public:
+ WebAssemblyLegalizerInfo(const WebAssemblySubtarget &ST);
+};
+} // namespace llvm
+#endif
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp
new file mode 100644
index 0000000000000..4345fbc66ab90
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp
@@ -0,0 +1,157 @@
+//=== WebAssemblyO0PreLegalizerCombiner.cpp -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass does combining of machine instructions at the generic MI level,
+// before the legalizer.
+//
+//===----------------------------------------------------------------------===//
+
+#include "WebAssembly.h"
+#include "WebAssemblySubtarget.h"
+#include "llvm/CodeGen/GlobalISel/Combiner.h"
+#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
+#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
+#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
+#include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"
+#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
+#include "llvm/CodeGen/MachineDominators.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+
+#define GET_GICOMBINER_DEPS
+#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_DEPS
+
+#define DEBUG_TYPE "wasm-O0-prelegalizer-combiner"
+
+using namespace llvm;
+
+namespace {
+#define GET_GICOMBINER_TYPES
+#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_TYPES
+
+class WebAssemblyO0PreLegalizerCombinerImpl : public Combiner {
+protected:
+ const CombinerHelper Helper;
+ const WebAssemblyO0PreLegalizerCombinerImplRuleConfig &RuleConfig;
+ const WebAssemblySubtarget &STI;
+
+public:
+ WebAssemblyO0PreLegalizerCombinerImpl(
+ MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
+ GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
+ const WebAssemblyO0PreLegalizerCombinerImplRuleConfig &RuleConfig,
+ const WebAssemblySubtarget &STI);
+
+ static const char *getName() { return "WebAssemblyO0PreLegalizerCombiner"; }
+
+ bool tryCombineAll(MachineInstr &I) const override;
+
+private:
+#define GET_GICOMBINER_CLASS_MEMBERS
+#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_CLASS_MEMBERS
+};
+
+#define GET_GICOMBINER_IMPL
+#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_IMPL
+
+WebAssemblyO0PreLegalizerCombinerImpl::WebAssemblyO0PreLegalizerCombinerImpl(
+ MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
+ GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
+ const WebAssemblyO0PreLegalizerCombinerImplRuleConfig &RuleConfig,
+ const WebAssemblySubtarget &STI)
+ : Combiner(MF, CInfo, TPC, &VT, CSEInfo),
+ Helper(Observer, B, /*IsPreLegalize*/ true, &VT), RuleConfig(RuleConfig),
+ STI(STI),
+#define GET_GICOMBINER_CONSTRUCTOR_INITS
+#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_CONSTRUCTOR_INITS
+{
+}
+
+// Pass boilerplate
+// ================
+
+class WebAssemblyO0PreLegalizerCombiner : public MachineFunctionPass {
+public:
+ static char ID;
+
+ WebAssemblyO0PreLegalizerCombiner();
+
+ StringRef getPassName() const override {
+ return "WebAssemblyO0PreLegalizerCombiner";
+ }
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override;
+
+private:
+ WebAssemblyO0PreLegalizerCombinerImplRuleConfig RuleConfig;
+};
+} // end anonymous namespace
+
+void WebAssemblyO0PreLegalizerCombiner::getAnalysisUsage(
+ AnalysisUsage &AU) const {
+ AU.addRequired<TargetPassConfig>();
+ AU.setPreservesCFG();
+ getSelectionDAGFallbackAnalysisUsage(AU);
+ AU.addRequired<GISelValueTrackingAnalysisLegacy>();
+ AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
+ MachineFunctionPass::getAnalysisUsage(AU);
+}
+
+WebAssemblyO0PreLegalizerCombiner::WebAssemblyO0PreLegalizerCombiner()
+ : MachineFunctionPass(ID) {
+ if (!RuleConfig.parseCommandLineOption())
+ report_fatal_error("Invalid rule identifier");
+}
+
+bool WebAssemblyO0PreLegalizerCombiner::runOnMachineFunction(
+ MachineFunction &MF) {
+ if (MF.getProperties().hasFailedISel())
+ return false;
+ auto &TPC = getAnalysis<TargetPassConfig>();
+
+ const Function &F = MF.getFunction();
+ GISelValueTracking *VT =
+ &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
+
+ const WebAssemblySubtarget &ST = MF.getSubtarget<WebAssemblySubtarget>();
+
+ CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
+ /*LegalizerInfo*/ nullptr, /*EnableOpt*/ false,
+ F.hasOptSize(), F.hasMinSize());
+ // Disable fixed-point iteration in the Combiner. This improves compile-time
+ // at the cost of possibly missing optimizations. See PR#94291 for details.
+ CInfo.MaxIterations = 1;
+
+ WebAssemblyO0PreLegalizerCombinerImpl Impl(MF, CInfo, &TPC, *VT,
+ /*CSEInfo*/ nullptr, RuleConfig,
+ ST);
+ return Impl.combineMachineInstrs();
+}
+
+char WebAssemblyO0PreLegalizerCombiner::ID = 0;
+INITIALIZE_PASS_BEGIN(WebAssemblyO0PreLegalizerCombiner, DEBUG_TYPE,
+ "Combine WebAssembly machine instrs before legalization",
+ false, false)
+INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
+INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
+INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)
+INITIALIZE_PASS_END(WebAssemblyO0PreLegalizerCombiner, DEBUG_TYPE,
+ "Combine WebAssembly machine instrs before legalization",
+ false, false)
+
+FunctionPass *llvm::createWebAssemblyO0PreLegalizerCombiner() {
+ return new WebAssemblyO0PreLegalizerCombiner();
+}
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp
new file mode 100644
index 0000000000000..a3ed3c207f332
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp
@@ -0,0 +1,182 @@
+//=== WebAssemblyPostLegalizerCombiner.cpp ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Post-legalization combines on generic MachineInstrs.
+///
+/// The combines here must preserve instruction legality.
+///
+/// Combines which don't rely on instruction legality should go in the
+/// WebAssemblyPreLegalizerCombiner.
+///
+//===----------------------------------------------------------------------===//
+
+#include "WebAssembly.h"
+#include "WebAssemblyTargetMachine.h"
+#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
+#include "llvm/CodeGen/GlobalISel/Combiner.h"
+#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
+#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
+#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
+#include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"
+#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
+#include "llvm/CodeGen/MachineDominators.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+
+#define GET_GICOMBINER_DEPS
+#include "WebAssemblyGenPostLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_DEPS
+
+#define DEBUG_TYPE "wasm-postlegalizer-combiner"
+
+using namespace llvm;
+
+namespace {
+
+#define GET_GICOMBINER_TYPES
+#include "WebAssemblyGenPostLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_TYPES
+
+class WebAssemblyPostLegalizerCombinerImpl : public Combiner {
+protected:
+ const CombinerHelper Helper;
+ const WebAssemblyPostLegalizerCombinerImplRuleConfig &RuleConfig;
+ const WebAssemblySubtarget &STI;
+
+public:
+ WebAssemblyPostLegalizerCombinerImpl(
+ MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
+ GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
+ const WebAssemblyPostLegalizerCombinerImplRuleConfig &RuleConfig,
+ const WebAssemblySubtarget &STI, MachineDominatorTree *MDT,
+ const LegalizerInfo *LI);
+
+ static const char *getName() { return "WebAssemblyPostLegalizerCombiner"; }
+
+ bool tryCombineAll(MachineInstr &I) const override;
+
+private:
+#define GET_GICOMBINER_CLASS_MEMBERS
+#include "WebAssemblyGenPostLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_CLASS_MEMBERS
+};
+
+#define GET_GICOMBINER_IMPL
+#include "WebAssemblyGenPostLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_IMPL
+
+WebAssemblyPostLegalizerCombinerImpl::WebAssemblyPostLegalizerCombinerImpl(
+ MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
+ GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
+ const WebAssemblyPostLegalizerCombinerImplRuleConfig &RuleConfig,
+ const WebAssemblySubtarget &STI, MachineDominatorTree *MDT,
+ const LegalizerInfo *LI)
+ : Combiner(MF, CInfo, TPC, &VT, CSEInfo),
+ Helper(Observer, B, /*IsPreLegalize*/ false, &VT, MDT, LI),
+ RuleConfig(RuleConfig), STI(STI),
+#define GET_GICOMBINER_CONSTRUCTOR_INITS
+#include "WebAssemblyGenPostLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_CONSTRUCTOR_INITS
+{
+}
+
+class WebAssemblyPostLegalizerCombiner : public MachineFunctionPass {
+public:
+ static char ID;
+
+ WebAssemblyPostLegalizerCombiner(bool IsOptNone = false);
+
+ StringRef getPassName() const override {
+ return "WebAssemblyPostLegalizerCombiner";
+ }
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+ void getAnalysisUsage(AnalysisUsage &AU) const override;
+
+private:
+ bool IsOptNone;
+ WebAssemblyPostLegalizerCombinerImplRuleConfig RuleConfig;
+};
+} // end anonymous namespace
+
+void WebAssemblyPostLegalizerCombiner::getAnalysisUsage(
+ AnalysisUsage &AU) const {
+ AU.addRequired<TargetPassConfig>();
+ AU.setPreservesCFG();
+ getSelectionDAGFallbackAnalysisUsage(AU);
+ AU.addRequired<GISelValueTrackingAnalysisLegacy>();
+ AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
+ if (!IsOptNone) {
+ AU.addRequired<MachineDominatorTreeWrapperPass>();
+ AU.addPreserved<MachineDominatorTreeWrapperPass>();
+ AU.addRequired<GISelCSEAnalysisWrapperPass>();
+ AU.addPreserved<GISelCSEAnalysisWrapperPass>();
+ }
+ MachineFunctionPass::getAnalysisUsage(AU);
+}
+
+WebAssemblyPostLegalizerCombiner::WebAssemblyPostLegalizerCombiner(
+ bool IsOptNone)
+ : MachineFunctionPass(ID), IsOptNone(IsOptNone) {
+ if (!RuleConfig.parseCommandLineOption())
+ report_fatal_error("Invalid rule identifier");
+}
+
+bool WebAssemblyPostLegalizerCombiner::runOnMachineFunction(
+ MachineFunction &MF) {
+ if (MF.getProperties().hasFailedISel())
+ return false;
+ assert(MF.getProperties().hasLegalized() && "Expected a legalized function?");
+ auto *TPC = &getAnalysis<TargetPassConfig>();
+ const Function &F = MF.getFunction();
+ bool EnableOpt =
+ MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);
+
+ const WebAssemblySubtarget &ST = MF.getSubtarget<WebAssemblySubtarget>();
+ const auto *LI = ST.getLegalizerInfo();
+
+ GISelValueTracking *VT =
+ &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
+ MachineDominatorTree *MDT = nullptr;
+ GISelCSEInfo *CSEInfo = nullptr;
+
+ if (!IsOptNone) {
+ MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
+
+ GISelCSEAnalysisWrapper &Wrapper =
+ getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
+ CSEInfo = &Wrapper.get(TPC->getCSEConfig());
+ }
+
+ CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
+ /*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),
+ F.hasMinSize());
+ // Disable fixed-point iteration to reduce compile-time
+ CInfo.MaxIterations = 1;
+ CInfo.ObserverLvl = CombinerInfo::ObserverLevel::SinglePass;
+ // Legalizer performs DCE, so a full DCE pass is unnecessary.
+ CInfo.EnableFullDCE = false;
+ WebAssemblyPostLegalizerCombinerImpl Impl(MF, CInfo, TPC, *VT, CSEInfo,
+ RuleConfig, ST, MDT, LI);
+ return Impl.combineMachineInstrs();
+}
+
+char WebAssemblyPostLegalizerCombiner::ID = 0;
+INITIALIZE_PASS_BEGIN(WebAssemblyPostLegalizerCombiner, DEBUG_TYPE,
+ "Combine WebAssembly MachineInstrs after legalization",
+ false, false)
+INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
+INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
+INITIALIZE_PASS_END(WebAssemblyPostLegalizerCombiner, DEBUG_TYPE,
+ "Combine WebAssembly MachineInstrs after legalization",
+ false, false)
+
+FunctionPass *llvm::createWebAssemblyPostLegalizerCombiner(bool IsOptNone) {
+ return new WebAssemblyPostLegalizerCombiner(IsOptNone);
+}
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPreLegalizerCombiner.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPreLegalizerCombiner.cpp
new file mode 100644
index 0000000000000..81f424db67227
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPreLegalizerCombiner.cpp
@@ -0,0 +1,176 @@
+//=== WebAssemblyPreLegalizerCombiner.cpp ---------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass does combining of machine instructions at the generic MI level,
+// before the legalizer.
+//
+//===----------------------------------------------------------------------===//
+
+#include "WebAssembly.h"
+#include "WebAssemblySubtarget.h"
+#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
+#include "llvm/CodeGen/GlobalISel/Combiner.h"
+#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
+#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
+#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
+#include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"
+#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
+#include "llvm/CodeGen/MachineDominators.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/Target/TargetMachine.h"
+
+#define GET_GICOMBINER_DEPS
+#include "WebAssemblyGenPreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_DEPS
+
+#define DEBUG_TYPE "wasm-prelegalizer-combiner"
+
+using namespace llvm;
+
+namespace {
+
+#define GET_GICOMBINER_TYPES
+#include "WebAssemblyGenPreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_TYPES
+
+class WebAssemblyPreLegalizerCombinerImpl : public Combiner {
+protected:
+ const CombinerHelper Helper;
+ const WebAssemblyPreLegalizerCombinerImplRuleConfig &RuleConfig;
+ const WebAssemblySubtarget &STI;
+
+public:
+ WebAssemblyPreLegalizerCombinerImpl(
+ MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
+ GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
+ const WebAssemblyPreLegalizerCombinerImplRuleConfig &RuleConfig,
+ const WebAssemblySubtarget &STI, MachineDominatorTree *MDT,
+ const LegalizerInfo *LI);
+
+ static const char *getName() { return "WebAssembly00PreLegalizerCombiner"; }
+
+ bool tryCombineAll(MachineInstr &I) const override;
+
+private:
+#define GET_GICOMBINER_CLASS_MEMBERS
+#include "WebAssemblyGenPreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_CLASS_MEMBERS
+};
+
+#define GET_GICOMBINER_IMPL
+#include "WebAssemblyGenPreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_IMPL
+
+WebAssemblyPreLegalizerCombinerImpl::WebAssemblyPreLegalizerCombinerImpl(
+ MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
+ GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
+ const WebAssemblyPreLegalizerCombinerImplRuleConfig &RuleConfig,
+ const WebAssemblySubtarget &STI, MachineDominatorTree *MDT,
+ const LegalizerInfo *LI)
+ : Combiner(MF, CInfo, TPC, &VT, CSEInfo),
+ Helper(Observer, B, /*IsPreLegalize*/ true, &VT, MDT, LI),
+ RuleConfig(RuleConfig), STI(STI),
+#define GET_GICOMBINER_CONSTRUCTOR_INITS
+#include "WebAssemblyGenPreLegalizeGICombiner.inc"
+#undef GET_GICOMBINER_CONSTRUCTOR_INITS
+{
+}
+
+// Pass boilerplate
+// ================
+
+class WebAssemblyPreLegalizerCombiner : public MachineFunctionPass {
+public:
+ static char ID;
+
+ WebAssemblyPreLegalizerCombiner();
+
+ StringRef getPassName() const override {
+ return "WebAssemblyPreLegalizerCombiner";
+ }
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override;
+
+private:
+ WebAssemblyPreLegalizerCombinerImplRuleConfig RuleConfig;
+};
+} // end anonymous namespace
+
+void WebAssemblyPreLegalizerCombiner::getAnalysisUsage(
+ AnalysisUsage &AU) const {
+ AU.addRequired<TargetPassConfig>();
+ AU.setPreservesCFG();
+ getSelectionDAGFallbackAnalysisUsage(AU);
+ AU.addRequired<GISelValueTrackingAnalysisLegacy>();
+ AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
+ AU.addRequired<MachineDominatorTreeWrapperPass>();
+ AU.addPreserved<MachineDominatorTreeWrapperPass>();
+ AU.addRequired<GISelCSEAnalysisWrapperPass>();
+ AU.addPreserved<GISelCSEAnalysisWrapperPass>();
+ MachineFunctionPass::getAnalysisUsage(AU);
+}
+
+WebAssemblyPreLegalizerCombiner::WebAssemblyPreLegalizerCombiner()
+ : MachineFunctionPass(ID) {
+ if (!RuleConfig.parseCommandLineOption())
+ report_fatal_error("Invalid rule identifier");
+}
+
+bool WebAssemblyPreLegalizerCombiner::runOnMachineFunction(
+ MachineFunction &MF) {
+ if (MF.getProperties().hasFailedISel())
+ return false;
+ auto &TPC = getAnalysis<TargetPassConfig>();
+
+ // Enable CSE.
+ GISelCSEAnalysisWrapper &Wrapper =
+ getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
+ auto *CSEInfo = &Wrapper.get(TPC.getCSEConfig());
+
+ const WebAssemblySubtarget &ST = MF.getSubtarget<WebAssemblySubtarget>();
+ const auto *LI = ST.getLegalizerInfo();
+
+ const Function &F = MF.getFunction();
+ bool EnableOpt =
+ MF.getTarget().getOptLevel() != CodeGenOptLevel::None && !skipFunction(F);
+ GISelValueTracking *VT =
+ &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
+ MachineDominatorTree *MDT =
+ &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
+ CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
+ /*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),
+ F.hasMinSize());
+ // Disable fixed-point iteration to reduce compile-time
+ CInfo.MaxIterations = 1;
+ CInfo.ObserverLvl = CombinerInfo::ObserverLevel::SinglePass;
+ // This is the first Combiner, so the input IR might contain dead
+ // instructions.
+ CInfo.EnableFullDCE = true;
+ WebAssemblyPreLegalizerCombinerImpl Impl(MF, CInfo, &TPC, *VT, CSEInfo,
+ RuleConfig, ST, MDT, LI);
+ return Impl.combineMachineInstrs();
+}
+
+char WebAssemblyPreLegalizerCombiner::ID = 0;
+INITIALIZE_PASS_BEGIN(WebAssemblyPreLegalizerCombiner, DEBUG_TYPE,
+ "Combine WebAssembly machine instrs before legalization",
+ false, false)
+INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
+INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
+INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)
+INITIALIZE_PASS_END(WebAssemblyPreLegalizerCombiner, DEBUG_TYPE,
+ "Combine WebAssembly machine instrs before legalization",
+ false, false)
+
+FunctionPass *llvm::createWebAssemblyPreLegalizerCombiner() {
+ return new WebAssemblyPreLegalizerCombiner();
+}
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.cpp
new file mode 100644
index 0000000000000..80a91ca35cf0f
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.cpp
@@ -0,0 +1,33 @@
+//===-- WebAssemblyRegisterBankInfo.cpp -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// This file implements the targeting of the RegisterBankInfo class for WASM.
+/// \todo This should be generated by TableGen.
+//===----------------------------------------------------------------------===//
+
+#include "WebAssemblyRegisterBankInfo.h"
+#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
+#include "llvm/CodeGen/TargetRegisterInfo.h"
+
+#define GET_TARGET_REGBANK_IMPL
+
+#include "WebAssemblyGenRegisterBank.inc"
+
+namespace llvm {
+namespace WebAssembly {} // namespace WebAssembly
+} // namespace llvm
+
+using namespace llvm;
+
+WebAssemblyRegisterBankInfo::WebAssemblyRegisterBankInfo(
+ const TargetRegisterInfo &TRI) {}
+
+const RegisterBankInfo::InstructionMapping &
+WebAssemblyRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
+ return getInvalidInstructionMapping();
+}
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.h b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.h
new file mode 100644
index 0000000000000..f0d95b56ef861
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyRegisterBankInfo.h
@@ -0,0 +1,40 @@
+//===- WebAssemblyRegisterBankInfo.h ----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// This file declares the targeting of the RegisterBankInfo class for WASM.
+/// \todo This should be generated by TableGen.
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYREGISTERBANKINFO_H
+#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLYREGISTERBANKINFO_H
+
+#include "llvm/CodeGen/RegisterBankInfo.h"
+
+#define GET_REGBANK_DECLARATIONS
+#include "WebAssemblyGenRegisterBank.inc"
+
+namespace llvm {
+
+class TargetRegisterInfo;
+
+class WebAssemblyGenRegisterBankInfo : public RegisterBankInfo {
+#define GET_TARGET_REGBANK_CLASS
+#include "WebAssemblyGenRegisterBank.inc"
+};
+
+/// This class provides the information for the target register banks.
+class WebAssemblyRegisterBankInfo final
+ : public WebAssemblyGenRegisterBankInfo {
+public:
+ WebAssemblyRegisterBankInfo(const TargetRegisterInfo &TRI);
+
+ const InstructionMapping &
+ getInstrMapping(const MachineInstr &MI) const override;
+};
+} // end namespace llvm
+#endif
diff --git a/llvm/lib/Target/WebAssembly/WebAssembly.h b/llvm/lib/Target/WebAssembly/WebAssembly.h
index 2dbd597f01cc9..7fbeffedc284b 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.h
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.h
@@ -15,6 +15,9 @@
#ifndef LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLY_H
#define LLVM_LIB_TARGET_WEBASSEMBLY_WEBASSEMBLY_H
+#include "GISel/WebAssemblyRegisterBankInfo.h"
+#include "WebAssemblySubtarget.h"
+#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
#include "llvm/PassRegistry.h"
#include "llvm/Support/CodeGen.h"
@@ -32,6 +35,21 @@ FunctionPass *createWebAssemblyOptimizeReturned();
FunctionPass *createWebAssemblyLowerRefTypesIntPtrConv();
FunctionPass *createWebAssemblyRefTypeMem2Local();
+// GlobalISel
+InstructionSelector *
+createWebAssemblyInstructionSelector(const WebAssemblyTargetMachine &,
+ const WebAssemblySubtarget &,
+ const WebAssemblyRegisterBankInfo &);
+
+FunctionPass *createWebAssemblyPostLegalizerCombiner(bool IsOptNone);
+void initializeWebAssemblyPostLegalizerCombinerPass(PassRegistry &);
+
+FunctionPass *createWebAssemblyO0PreLegalizerCombiner();
+void initializeWebAssemblyO0PreLegalizerCombinerPass(PassRegistry &);
+
+FunctionPass *createWebAssemblyPreLegalizerCombiner();
+void initializeWebAssemblyPreLegalizerCombinerPass(PassRegistry &);
+
// ISel and immediate followup passes.
FunctionPass *createWebAssemblyISelDag(WebAssemblyTargetMachine &TM,
CodeGenOptLevel OptLevel);
diff --git a/llvm/lib/Target/WebAssembly/WebAssembly.td b/llvm/lib/Target/WebAssembly/WebAssembly.td
index 67015ffcfc760..67a2331b26b6e 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.td
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.td
@@ -101,6 +101,7 @@ def FeatureWideArithmetic :
//===----------------------------------------------------------------------===//
include "WebAssemblyRegisterInfo.td"
+include "WebAssemblyRegisterBanks.td"
//===----------------------------------------------------------------------===//
// Instruction Descriptions
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCombine.td b/llvm/lib/Target/WebAssembly/WebAssemblyCombine.td
new file mode 100644
index 0000000000000..c079b0ae95f42
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCombine.td
@@ -0,0 +1,26 @@
+//=- WebAssemblyCombine.td - Define WASM Combine Rules -------*- tablegen -*-=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//
+//===----------------------------------------------------------------------===//
+
+include "llvm/Target/GlobalISel/Combine.td"
+
+def WebAssemblyPreLegalizerCombiner: GICombiner<
+ "WebAssemblyPreLegalizerCombinerImpl", []> {
+}
+
+def WebAssemblyO0PreLegalizerCombiner: GICombiner<
+ "WebAssemblyO0PreLegalizerCombinerImpl", []> {
+}
+
+// Post-legalization combines which are primarily optimizations.
+def WebAssemblyPostLegalizerCombiner
+ : GICombiner<"WebAssemblyPostLegalizerCombinerImpl",
+ []> {
+}
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyGISel.td b/llvm/lib/Target/WebAssembly/WebAssemblyGISel.td
new file mode 100644
index 0000000000000..3f6d3d308f9cf
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyGISel.td
@@ -0,0 +1,17 @@
+//===-- WebAssemblyGISel.td - WASM GlobalISel Patterns -----*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+/// \file
+/// This file contains patterns that are relevant to GlobalISel, including
+/// GIComplexOperandMatcher definitions for equivalent SelectionDAG
+/// ComplexPatterns.
+//
+//===----------------------------------------------------------------------===//
+
+include "WebAssembly.td"
+include "WebAssemblyCombine.td"
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyRegisterBanks.td b/llvm/lib/Target/WebAssembly/WebAssemblyRegisterBanks.td
new file mode 100644
index 0000000000000..b7c33c8076d00
--- /dev/null
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyRegisterBanks.td
@@ -0,0 +1,22 @@
+//=- WebAssemblyRegisterBank.td - Describe the WASM Banks ----*- tablegen -*-=//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+//
+//===----------------------------------------------------------------------===//
+
+
+def I32RegBank : RegisterBank<"I32RegBank", [I32]>;
+def I64RegBank : RegisterBank<"I64RegBank", [I64]>;
+def F32RegBank : RegisterBank<"F32RegBank", [F32]>;
+def F64RegBank : RegisterBank<"F64RegBank", [F64]>;
+
+def V128RegBank : RegisterBank<"V128RegBank", [V128]>;
+
+def EXTERNREFRegBank : RegisterBank<"EXTERNREFRegBank", [EXTERNREF]>;
+def FUNCREFRegBank : RegisterBank<"FUNCREFRegBank", [FUNCREF]>;
+def EXNREFRegBank : RegisterBank<"EXNREFRegBank", [EXNREF]>;
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp
index a3ce40f0297ec..641eef73044cd 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp
@@ -13,8 +13,13 @@
//===----------------------------------------------------------------------===//
#include "WebAssemblySubtarget.h"
+#include "GISel/WebAssemblyCallLowering.h"
+#include "GISel/WebAssemblyLegalizerInfo.h"
+#include "GISel/WebAssemblyRegisterBankInfo.h"
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
+#include "WebAssembly.h"
#include "WebAssemblyInstrInfo.h"
+#include "WebAssemblyTargetMachine.h"
#include "llvm/MC/TargetRegistry.h"
using namespace llvm;
@@ -66,7 +71,15 @@ WebAssemblySubtarget::WebAssemblySubtarget(const Triple &TT,
const TargetMachine &TM)
: WebAssemblyGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS),
TargetTriple(TT), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
- TLInfo(TM, *this) {}
+ TLInfo(TM, *this) {
+ CallLoweringInfo.reset(new WebAssemblyCallLowering(*getTargetLowering()));
+ Legalizer.reset(new WebAssemblyLegalizerInfo(*this));
+ auto *RBI = new WebAssemblyRegisterBankInfo(*getRegisterInfo());
+ RegBankInfo.reset(RBI);
+
+ InstSelector.reset(createWebAssemblyInstructionSelector(
+ *static_cast<const WebAssemblyTargetMachine *>(&TM), *this, *RBI));
+}
bool WebAssemblySubtarget::enableAtomicExpand() const {
// If atomics are disabled, atomic ops are lowered instead of expanded
@@ -81,3 +94,19 @@ bool WebAssemblySubtarget::enableMachineScheduler() const {
}
bool WebAssemblySubtarget::useAA() const { return true; }
+
+const CallLowering *WebAssemblySubtarget::getCallLowering() const {
+ return CallLoweringInfo.get();
+}
+
+InstructionSelector *WebAssemblySubtarget::getInstructionSelector() const {
+ return InstSelector.get();
+}
+
+const LegalizerInfo *WebAssemblySubtarget::getLegalizerInfo() const {
+ return Legalizer.get();
+}
+
+const RegisterBankInfo *WebAssemblySubtarget::getRegBankInfo() const {
+ return RegBankInfo.get();
+}
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h
index 2f88bbba05d00..c195f995009b1 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h
@@ -20,6 +20,10 @@
#include "WebAssemblyISelLowering.h"
#include "WebAssemblyInstrInfo.h"
#include "WebAssemblySelectionDAGInfo.h"
+#include "llvm/CodeGen/GlobalISel/CallLowering.h"
+#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
+#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
+#include "llvm/CodeGen/RegisterBankInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include <string>
@@ -64,6 +68,11 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
WebAssemblySelectionDAGInfo TSInfo;
WebAssemblyTargetLowering TLInfo;
+ std::unique_ptr<CallLowering> CallLoweringInfo;
+ std::unique_ptr<InstructionSelector> InstSelector;
+ std::unique_ptr<LegalizerInfo> Legalizer;
+ std::unique_ptr<RegisterBankInfo> RegBankInfo;
+
WebAssemblySubtarget &initializeSubtargetDependencies(StringRef CPU,
StringRef FS);
@@ -118,6 +127,11 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
/// Parses features string setting specified subtarget options. Definition of
/// function is auto generated by tblgen.
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
+
+ const CallLowering *getCallLowering() const override;
+ InstructionSelector *getInstructionSelector() const override;
+ const LegalizerInfo *getLegalizerInfo() const override;
+ const RegisterBankInfo *getRegBankInfo() const override;
};
} // end namespace llvm
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index a57ebfad03b1c..5dee8c2914a29 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -20,6 +20,10 @@
#include "WebAssemblyTargetObjectFile.h"
#include "WebAssemblyTargetTransformInfo.h"
#include "WebAssemblyUtilities.h"
+#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
+#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
+#include "llvm/CodeGen/GlobalISel/Legalizer.h"
+#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
#include "llvm/CodeGen/MIRParser/MIParser.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/RegAllocRegistry.h"
@@ -92,6 +96,10 @@ LLVMInitializeWebAssemblyTarget() {
// Register backend passes
auto &PR = *PassRegistry::getPassRegistry();
+ initializeGlobalISel(PR);
+ initializeWebAssemblyO0PreLegalizerCombinerPass(PR);
+ initializeWebAssemblyPreLegalizerCombinerPass(PR);
+ initializeWebAssemblyPostLegalizerCombinerPass(PR);
initializeWebAssemblyAddMissingPrototypesPass(PR);
initializeWebAssemblyLowerEmscriptenEHSjLjPass(PR);
initializeLowerGlobalDtorsLegacyPassPass(PR);
@@ -442,6 +450,13 @@ class WebAssemblyPassConfig final : public TargetPassConfig {
// No reg alloc
bool addRegAssignAndRewriteOptimized() override { return false; }
+
+ bool addIRTranslator() override;
+ void addPreLegalizeMachineIR() override;
+ bool addLegalizeMachineIR() override;
+ void addPreRegBankSelect() override;
+ bool addRegBankSelect() override;
+ bool addGlobalInstructionSelect() override;
};
} // end anonymous namespace
@@ -662,6 +677,47 @@ bool WebAssemblyPassConfig::addPreISel() {
return false;
}
+bool WebAssemblyPassConfig::addIRTranslator() {
+ addPass(new IRTranslator());
+ return false;
+}
+
+void WebAssemblyPassConfig::addPreLegalizeMachineIR() {
+ if (getOptLevel() == CodeGenOptLevel::None) {
+ addPass(createWebAssemblyO0PreLegalizerCombiner());
+ } else {
+ addPass(createWebAssemblyPreLegalizerCombiner());
+ }
+}
+bool WebAssemblyPassConfig::addLegalizeMachineIR() {
+ addPass(new Legalizer());
+ return false;
+}
+
+void WebAssemblyPassConfig::addPreRegBankSelect() {
+ addPass(createWebAssemblyPostLegalizerCombiner(getOptLevel() ==
+ CodeGenOptLevel::None));
+}
+
+bool WebAssemblyPassConfig::addRegBankSelect() {
+ addPass(new RegBankSelect());
+ return false;
+}
+
+bool WebAssemblyPassConfig::addGlobalInstructionSelect() {
+ addPass(new InstructionSelect(getOptLevel()));
+
+ // We insert only if ISelDAG won't insert these at a later point.
+ if (isGlobalISelAbortEnabled()) {
+ addPass(createWebAssemblyArgumentMove());
+ addPass(createWebAssemblySetP2AlignOperands());
+ addPass(createWebAssemblyFixBrTableDefaults());
+ addPass(createWebAssemblyCleanCodeAfterTrap());
+ }
+
+ return false;
+}
+
yaml::MachineFunctionInfo *
WebAssemblyTargetMachine::createDefaultFuncInfoYAML() const {
return new yaml::WebAssemblyFunctionInfo();
diff --git a/llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll b/llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll
new file mode 100644
index 0000000000000..7118f919b8e76
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll
@@ -0,0 +1,53 @@
+; RUN: llc -mtriple=wasm32-- -debug-pass=Structure %s -o /dev/null 2>&1 \
+; RUN: -verify-machineinstrs=0 -O0 -global-isel \
+; RUN: | FileCheck %s --check-prefixes=ENABLED,NOFALLBACK,ENABLED-O0
+
+; RUN: llc -mtriple=wasm32-- -debug-pass=Structure %s -o /dev/null 2>&1 \
+; RUN: -verify-machineinstrs=0 -global-isel \
+; RUN: | FileCheck %s --check-prefixes=ENABLED,NOFALLBACK,ENABLED-O1
+
+; RUN: llc -mtriple=wasm32-- -debug-pass=Structure %s -o /dev/null 2>&1 \
+; RUN: -verify-machineinstrs=0 -global-isel -global-isel-abort=2 \
+; RUN: | FileCheck %s --check-prefixes=ENABLED,FALLBACK,ENABLED-O1
+
+; RUN: llc -mtriple=wasm32-- -debug-pass=Structure %s -o /dev/null 2>&1 \
+; RUN: -verify-machineinstrs=0 \
+; RUN: | FileCheck %s --check-prefixes=DISABLED
+
+; ENABLED: IRTranslator
+; ENABLED-NEXT: Analysis for ComputingKnownBits
+; ENABLED-O0-NEXT: WebAssemblyO0PreLegalizerCombiner
+; ENABLED-O1-NEXT: MachineDominator Tree Construction
+; ENABLED-NEXT: Analysis containing CSE Info
+; ENABLED-O1-NEXT: WebAssemblyPreLegalizerCombiner
+; ENABLED-NEXT: Legalizer
+; ENABLED-O1-NEXT: MachineDominator Tree Construction
+; ENABLED-NEXT: WebAssemblyPostLegalizerCombiner
+; ENABLED-NEXT: RegBankSelect
+; ENABLED-NEXT: Analysis for ComputingKnownBits
+; ENABLED-O1-NEXT: Natural Loop Information
+; ENABLED-O1-NEXT: Lazy Branch Probability Analysis
+; ENABLED-O1-NEXT: Lazy Block Frequency Analysis
+; ENABLED-NEXT: InstructionSelect
+; NOFALLBACK-NEXT: WebAssembly Argument Move
+; NOFALLBACK-NEXT: WebAssembly Set p2align Operands
+; NOFALLBACK-NEXT: WebAssembly Fix br_table Defaults
+; NOFALLBACK-NEXT: WebAssembly Clean Code After Trap
+; ENABLED-NEXT: ResetMachineFunction
+
+; FALLBACK: WebAssembly Instruction Selection
+; FALLBACK-NEXT: WebAssembly Argument Move
+; FALLBACK-NEXT: WebAssembly Set p2align Operands
+; FALLBACK-NEXT: WebAssembly Fix br_table Defaults
+; FALLBACK-NEXT: WebAssembly Clean Code After Trap
+
+; NOFALLBACK-NOT: WebAssembly Instruction Selection
+
+; DISABLED-NOT: IRTranslator
+
+; DISABLED: WebAssembly Instruction Selection
+; DISABLED: Finalize ISel and expand pseudo-instructions
+
+define void @empty() {
+ ret void
+}
>From 4679818124023f22edb3d020de7f3a0c0c721173 Mon Sep 17 00:00:00 2001
From: Demetrius Kanios <demetrius at kanios.net>
Date: Thu, 29 Jan 2026 16:35:37 -0800
Subject: [PATCH 2/3] Fix warning/error due to unused member in
`WebAssemblyInstructionSelector`
---
.../WebAssembly/GISel/WebAssemblyInstructionSelector.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp
index 97caaa1d504ae..d6eee444af992 100644
--- a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyInstructionSelector.cpp
@@ -48,7 +48,7 @@ class WebAssemblyInstructionSelector : public InstructionSelector {
bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
const WebAssemblyTargetMachine &TM;
- const WebAssemblySubtarget &STI;
+ // const WebAssemblySubtarget &STI;
const WebAssemblyInstrInfo &TII;
const WebAssemblyRegisterInfo &TRI;
const WebAssemblyRegisterBankInfo &RBI;
@@ -71,8 +71,8 @@ class WebAssemblyInstructionSelector : public InstructionSelector {
WebAssemblyInstructionSelector::WebAssemblyInstructionSelector(
const WebAssemblyTargetMachine &TM, const WebAssemblySubtarget &STI,
const WebAssemblyRegisterBankInfo &RBI)
- : TM(TM), STI(STI), TII(*STI.getInstrInfo()), TRI(*STI.getRegisterInfo()),
- RBI(RBI),
+ : TM(TM), /*STI(STI),*/ TII(*STI.getInstrInfo()),
+ TRI(*STI.getRegisterInfo()), RBI(RBI),
#define GET_GLOBALISEL_PREDICATES_INIT
#include "WebAssemblyGenGlobalISel.inc"
>From d240d42d662b4fbb1affab85bb51ec0bebb66eaf Mon Sep 17 00:00:00 2001
From: Demetrius Kanios <demetrius at kanios.net>
Date: Mon, 2 Feb 2026 11:57:06 -0800
Subject: [PATCH 3/3] Remove combiners for O0
---
llvm/lib/Target/WebAssembly/CMakeLists.txt | 3 -
.../WebAssemblyO0PreLegalizerCombiner.cpp | 157 ------------------
.../WebAssemblyPostLegalizerCombiner.cpp | 32 ++--
llvm/lib/Target/WebAssembly/WebAssembly.h | 5 +-
.../Target/WebAssembly/WebAssemblyCombine.td | 4 -
.../WebAssembly/WebAssemblyTargetMachine.cpp | 10 +-
.../GlobalISel/gisel-commandline-option.ll | 6 +-
7 files changed, 21 insertions(+), 196 deletions(-)
delete mode 100644 llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp
diff --git a/llvm/lib/Target/WebAssembly/CMakeLists.txt b/llvm/lib/Target/WebAssembly/CMakeLists.txt
index 217df5f987e85..1ad99f7e468df 100644
--- a/llvm/lib/Target/WebAssembly/CMakeLists.txt
+++ b/llvm/lib/Target/WebAssembly/CMakeLists.txt
@@ -16,8 +16,6 @@ tablegen(LLVM WebAssemblyGenSubtargetInfo.inc -gen-subtarget)
set(LLVM_TARGET_DEFINITIONS WebAssemblyGISel.td)
tablegen(LLVM WebAssemblyGenGlobalISel.inc -gen-global-isel)
-tablegen(LLVM WebAssemblyGenO0PreLegalizeGICombiner.inc -gen-global-isel-combiner
- -combiners="WebAssemblyO0PreLegalizerCombiner")
tablegen(LLVM WebAssemblyGenPreLegalizeGICombiner.inc -gen-global-isel-combiner
-combiners="WebAssemblyPreLegalizerCombiner")
tablegen(LLVM WebAssemblyGenPostLegalizeGICombiner.inc -gen-global-isel-combiner
@@ -28,7 +26,6 @@ add_public_tablegen_target(WebAssemblyCommonTableGen)
add_llvm_target(WebAssemblyCodeGen
GISel/WebAssemblyCallLowering.cpp
GISel/WebAssemblyInstructionSelector.cpp
- GISel/WebAssemblyO0PreLegalizerCombiner.cpp
GISel/WebAssemblyPostLegalizerCombiner.cpp
GISel/WebAssemblyPreLegalizerCombiner.cpp
GISel/WebAssemblyLegalizerInfo.cpp
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp
deleted file mode 100644
index 4345fbc66ab90..0000000000000
--- a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyO0PreLegalizerCombiner.cpp
+++ /dev/null
@@ -1,157 +0,0 @@
-//=== WebAssemblyO0PreLegalizerCombiner.cpp -------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// This pass does combining of machine instructions at the generic MI level,
-// before the legalizer.
-//
-//===----------------------------------------------------------------------===//
-
-#include "WebAssembly.h"
-#include "WebAssemblySubtarget.h"
-#include "llvm/CodeGen/GlobalISel/Combiner.h"
-#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
-#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
-#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
-#include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"
-#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
-#include "llvm/CodeGen/MachineDominators.h"
-#include "llvm/CodeGen/MachineFunction.h"
-#include "llvm/CodeGen/MachineFunctionPass.h"
-#include "llvm/CodeGen/TargetPassConfig.h"
-
-#define GET_GICOMBINER_DEPS
-#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
-#undef GET_GICOMBINER_DEPS
-
-#define DEBUG_TYPE "wasm-O0-prelegalizer-combiner"
-
-using namespace llvm;
-
-namespace {
-#define GET_GICOMBINER_TYPES
-#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
-#undef GET_GICOMBINER_TYPES
-
-class WebAssemblyO0PreLegalizerCombinerImpl : public Combiner {
-protected:
- const CombinerHelper Helper;
- const WebAssemblyO0PreLegalizerCombinerImplRuleConfig &RuleConfig;
- const WebAssemblySubtarget &STI;
-
-public:
- WebAssemblyO0PreLegalizerCombinerImpl(
- MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
- GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
- const WebAssemblyO0PreLegalizerCombinerImplRuleConfig &RuleConfig,
- const WebAssemblySubtarget &STI);
-
- static const char *getName() { return "WebAssemblyO0PreLegalizerCombiner"; }
-
- bool tryCombineAll(MachineInstr &I) const override;
-
-private:
-#define GET_GICOMBINER_CLASS_MEMBERS
-#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
-#undef GET_GICOMBINER_CLASS_MEMBERS
-};
-
-#define GET_GICOMBINER_IMPL
-#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
-#undef GET_GICOMBINER_IMPL
-
-WebAssemblyO0PreLegalizerCombinerImpl::WebAssemblyO0PreLegalizerCombinerImpl(
- MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC,
- GISelValueTracking &VT, GISelCSEInfo *CSEInfo,
- const WebAssemblyO0PreLegalizerCombinerImplRuleConfig &RuleConfig,
- const WebAssemblySubtarget &STI)
- : Combiner(MF, CInfo, TPC, &VT, CSEInfo),
- Helper(Observer, B, /*IsPreLegalize*/ true, &VT), RuleConfig(RuleConfig),
- STI(STI),
-#define GET_GICOMBINER_CONSTRUCTOR_INITS
-#include "WebAssemblyGenO0PreLegalizeGICombiner.inc"
-#undef GET_GICOMBINER_CONSTRUCTOR_INITS
-{
-}
-
-// Pass boilerplate
-// ================
-
-class WebAssemblyO0PreLegalizerCombiner : public MachineFunctionPass {
-public:
- static char ID;
-
- WebAssemblyO0PreLegalizerCombiner();
-
- StringRef getPassName() const override {
- return "WebAssemblyO0PreLegalizerCombiner";
- }
-
- bool runOnMachineFunction(MachineFunction &MF) override;
-
- void getAnalysisUsage(AnalysisUsage &AU) const override;
-
-private:
- WebAssemblyO0PreLegalizerCombinerImplRuleConfig RuleConfig;
-};
-} // end anonymous namespace
-
-void WebAssemblyO0PreLegalizerCombiner::getAnalysisUsage(
- AnalysisUsage &AU) const {
- AU.addRequired<TargetPassConfig>();
- AU.setPreservesCFG();
- getSelectionDAGFallbackAnalysisUsage(AU);
- AU.addRequired<GISelValueTrackingAnalysisLegacy>();
- AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
- MachineFunctionPass::getAnalysisUsage(AU);
-}
-
-WebAssemblyO0PreLegalizerCombiner::WebAssemblyO0PreLegalizerCombiner()
- : MachineFunctionPass(ID) {
- if (!RuleConfig.parseCommandLineOption())
- report_fatal_error("Invalid rule identifier");
-}
-
-bool WebAssemblyO0PreLegalizerCombiner::runOnMachineFunction(
- MachineFunction &MF) {
- if (MF.getProperties().hasFailedISel())
- return false;
- auto &TPC = getAnalysis<TargetPassConfig>();
-
- const Function &F = MF.getFunction();
- GISelValueTracking *VT =
- &getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);
-
- const WebAssemblySubtarget &ST = MF.getSubtarget<WebAssemblySubtarget>();
-
- CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
- /*LegalizerInfo*/ nullptr, /*EnableOpt*/ false,
- F.hasOptSize(), F.hasMinSize());
- // Disable fixed-point iteration in the Combiner. This improves compile-time
- // at the cost of possibly missing optimizations. See PR#94291 for details.
- CInfo.MaxIterations = 1;
-
- WebAssemblyO0PreLegalizerCombinerImpl Impl(MF, CInfo, &TPC, *VT,
- /*CSEInfo*/ nullptr, RuleConfig,
- ST);
- return Impl.combineMachineInstrs();
-}
-
-char WebAssemblyO0PreLegalizerCombiner::ID = 0;
-INITIALIZE_PASS_BEGIN(WebAssemblyO0PreLegalizerCombiner, DEBUG_TYPE,
- "Combine WebAssembly machine instrs before legalization",
- false, false)
-INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
-INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)
-INITIALIZE_PASS_DEPENDENCY(GISelCSEAnalysisWrapperPass)
-INITIALIZE_PASS_END(WebAssemblyO0PreLegalizerCombiner, DEBUG_TYPE,
- "Combine WebAssembly machine instrs before legalization",
- false, false)
-
-FunctionPass *llvm::createWebAssemblyO0PreLegalizerCombiner() {
- return new WebAssemblyO0PreLegalizerCombiner();
-}
diff --git a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp
index a3ed3c207f332..aa7e4c4e91136 100644
--- a/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp
+++ b/llvm/lib/Target/WebAssembly/GISel/WebAssemblyPostLegalizerCombiner.cpp
@@ -90,7 +90,7 @@ class WebAssemblyPostLegalizerCombiner : public MachineFunctionPass {
public:
static char ID;
- WebAssemblyPostLegalizerCombiner(bool IsOptNone = false);
+ WebAssemblyPostLegalizerCombiner();
StringRef getPassName() const override {
return "WebAssemblyPostLegalizerCombiner";
@@ -100,7 +100,6 @@ class WebAssemblyPostLegalizerCombiner : public MachineFunctionPass {
void getAnalysisUsage(AnalysisUsage &AU) const override;
private:
- bool IsOptNone;
WebAssemblyPostLegalizerCombinerImplRuleConfig RuleConfig;
};
} // end anonymous namespace
@@ -112,18 +111,15 @@ void WebAssemblyPostLegalizerCombiner::getAnalysisUsage(
getSelectionDAGFallbackAnalysisUsage(AU);
AU.addRequired<GISelValueTrackingAnalysisLegacy>();
AU.addPreserved<GISelValueTrackingAnalysisLegacy>();
- if (!IsOptNone) {
- AU.addRequired<MachineDominatorTreeWrapperPass>();
- AU.addPreserved<MachineDominatorTreeWrapperPass>();
- AU.addRequired<GISelCSEAnalysisWrapperPass>();
- AU.addPreserved<GISelCSEAnalysisWrapperPass>();
- }
+ AU.addRequired<MachineDominatorTreeWrapperPass>();
+ AU.addPreserved<MachineDominatorTreeWrapperPass>();
+ AU.addRequired<GISelCSEAnalysisWrapperPass>();
+ AU.addPreserved<GISelCSEAnalysisWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}
-WebAssemblyPostLegalizerCombiner::WebAssemblyPostLegalizerCombiner(
- bool IsOptNone)
- : MachineFunctionPass(ID), IsOptNone(IsOptNone) {
+WebAssemblyPostLegalizerCombiner::WebAssemblyPostLegalizerCombiner()
+ : MachineFunctionPass(ID) {
if (!RuleConfig.parseCommandLineOption())
report_fatal_error("Invalid rule identifier");
}
@@ -146,13 +142,11 @@ bool WebAssemblyPostLegalizerCombiner::runOnMachineFunction(
MachineDominatorTree *MDT = nullptr;
GISelCSEInfo *CSEInfo = nullptr;
- if (!IsOptNone) {
- MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
+ MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
- GISelCSEAnalysisWrapper &Wrapper =
- getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
- CSEInfo = &Wrapper.get(TPC->getCSEConfig());
- }
+ GISelCSEAnalysisWrapper &Wrapper =
+ getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
+ CSEInfo = &Wrapper.get(TPC->getCSEConfig());
CombinerInfo CInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
/*LegalizerInfo*/ nullptr, EnableOpt, F.hasOptSize(),
@@ -177,6 +171,6 @@ INITIALIZE_PASS_END(WebAssemblyPostLegalizerCombiner, DEBUG_TYPE,
"Combine WebAssembly MachineInstrs after legalization",
false, false)
-FunctionPass *llvm::createWebAssemblyPostLegalizerCombiner(bool IsOptNone) {
- return new WebAssemblyPostLegalizerCombiner(IsOptNone);
+FunctionPass *llvm::createWebAssemblyPostLegalizerCombiner() {
+ return new WebAssemblyPostLegalizerCombiner();
}
diff --git a/llvm/lib/Target/WebAssembly/WebAssembly.h b/llvm/lib/Target/WebAssembly/WebAssembly.h
index 7fbeffedc284b..71647974b2843 100644
--- a/llvm/lib/Target/WebAssembly/WebAssembly.h
+++ b/llvm/lib/Target/WebAssembly/WebAssembly.h
@@ -41,12 +41,9 @@ createWebAssemblyInstructionSelector(const WebAssemblyTargetMachine &,
const WebAssemblySubtarget &,
const WebAssemblyRegisterBankInfo &);
-FunctionPass *createWebAssemblyPostLegalizerCombiner(bool IsOptNone);
+FunctionPass *createWebAssemblyPostLegalizerCombiner();
void initializeWebAssemblyPostLegalizerCombinerPass(PassRegistry &);
-FunctionPass *createWebAssemblyO0PreLegalizerCombiner();
-void initializeWebAssemblyO0PreLegalizerCombinerPass(PassRegistry &);
-
FunctionPass *createWebAssemblyPreLegalizerCombiner();
void initializeWebAssemblyPreLegalizerCombinerPass(PassRegistry &);
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCombine.td b/llvm/lib/Target/WebAssembly/WebAssemblyCombine.td
index c079b0ae95f42..83c1482863e09 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCombine.td
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCombine.td
@@ -15,10 +15,6 @@ def WebAssemblyPreLegalizerCombiner: GICombiner<
"WebAssemblyPreLegalizerCombinerImpl", []> {
}
-def WebAssemblyO0PreLegalizerCombiner: GICombiner<
- "WebAssemblyO0PreLegalizerCombinerImpl", []> {
-}
-
// Post-legalization combines which are primarily optimizations.
def WebAssemblyPostLegalizerCombiner
: GICombiner<"WebAssemblyPostLegalizerCombinerImpl",
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index 5dee8c2914a29..ab1b24a4fd9b1 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -97,7 +97,6 @@ LLVMInitializeWebAssemblyTarget() {
// Register backend passes
auto &PR = *PassRegistry::getPassRegistry();
initializeGlobalISel(PR);
- initializeWebAssemblyO0PreLegalizerCombinerPass(PR);
initializeWebAssemblyPreLegalizerCombinerPass(PR);
initializeWebAssemblyPostLegalizerCombinerPass(PR);
initializeWebAssemblyAddMissingPrototypesPass(PR);
@@ -683,9 +682,7 @@ bool WebAssemblyPassConfig::addIRTranslator() {
}
void WebAssemblyPassConfig::addPreLegalizeMachineIR() {
- if (getOptLevel() == CodeGenOptLevel::None) {
- addPass(createWebAssemblyO0PreLegalizerCombiner());
- } else {
+ if (getOptLevel() != CodeGenOptLevel::None) {
addPass(createWebAssemblyPreLegalizerCombiner());
}
}
@@ -695,8 +692,9 @@ bool WebAssemblyPassConfig::addLegalizeMachineIR() {
}
void WebAssemblyPassConfig::addPreRegBankSelect() {
- addPass(createWebAssemblyPostLegalizerCombiner(getOptLevel() ==
- CodeGenOptLevel::None));
+ if (getOptLevel() != CodeGenOptLevel::None) {
+ addPass(createWebAssemblyPostLegalizerCombiner());
+ }
}
bool WebAssemblyPassConfig::addRegBankSelect() {
diff --git a/llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll b/llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll
index 7118f919b8e76..47b05c8160fd1 100644
--- a/llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll
+++ b/llvm/test/CodeGen/WebAssembly/GlobalISel/gisel-commandline-option.ll
@@ -15,14 +15,14 @@
; RUN: | FileCheck %s --check-prefixes=DISABLED
; ENABLED: IRTranslator
+; ENABLED-O0-NEXT: Analysis containing CSE Info
; ENABLED-NEXT: Analysis for ComputingKnownBits
-; ENABLED-O0-NEXT: WebAssemblyO0PreLegalizerCombiner
; ENABLED-O1-NEXT: MachineDominator Tree Construction
-; ENABLED-NEXT: Analysis containing CSE Info
+; ENABLED-O1-NEXT: Analysis containing CSE Info
; ENABLED-O1-NEXT: WebAssemblyPreLegalizerCombiner
; ENABLED-NEXT: Legalizer
; ENABLED-O1-NEXT: MachineDominator Tree Construction
-; ENABLED-NEXT: WebAssemblyPostLegalizerCombiner
+; ENABLED-O1-NEXT: WebAssemblyPostLegalizerCombiner
; ENABLED-NEXT: RegBankSelect
; ENABLED-NEXT: Analysis for ComputingKnownBits
; ENABLED-O1-NEXT: Natural Loop Information
More information about the llvm-commits
mailing list