Skip to content

Commit

Permalink
[AArch64] Use GlobalISel MatchTable Combiner Backend
Browse files Browse the repository at this point in the history
Only a few minor test changes needed because I removed the "helper" suffix from the combiner name, as it's not really a helper anymore but more like the implementation itself.

Depends on D153757

NOTE: This would land iff D153757 (RFC) lands too.

Reviewed By: aemerson

Differential Revision: https://reviews.llvm.org/D153850
  • Loading branch information
Pierre-vh committed Jul 11, 2023
1 parent 8444038 commit 655714a
Show file tree
Hide file tree
Showing 27 changed files with 358 additions and 246 deletions.
30 changes: 11 additions & 19 deletions llvm/lib/Target/AArch64/AArch64Combine.td
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,15 @@ def fold_global_offset : GICombineRule<
(apply [{ applyFoldGlobalOffset(*${root}, MRI, B, Observer, ${matchinfo});}])
>;

def AArch64PreLegalizerCombinerHelper: GICombinerHelper<
"AArch64GenPreLegalizerCombinerHelper", [all_combines,
fconstant_to_constant,
icmp_redundant_trunc,
fold_global_offset]> {
let DisableRuleOption = "aarch64prelegalizercombiner-disable-rule";
let StateClass = "AArch64PreLegalizerCombinerHelperState";
let AdditionalArguments = [];
def AArch64PreLegalizerCombiner: GICombinerHelper<
"AArch64PreLegalizerCombinerImpl", [all_combines,
fconstant_to_constant,
icmp_redundant_trunc,
fold_global_offset]> {
}

def AArch64O0PreLegalizerCombinerHelper: GICombinerHelper<
"AArch64GenO0PreLegalizerCombinerHelper", [optnone_combines]> {
let DisableRuleOption = "aarch64O0prelegalizercombiner-disable-rule";
let StateClass = "AArch64O0PreLegalizerCombinerHelperState";
let AdditionalArguments = [];
def AArch64O0PreLegalizerCombiner: GICombinerHelper<
"AArch64O0PreLegalizerCombinerImpl", [optnone_combines]> {
}

// Matchdata for combines which replace a G_SHUFFLE_VECTOR with a
Expand Down Expand Up @@ -213,18 +207,17 @@ def vector_sext_inreg_to_shift : GICombineRule<
// Post-legalization combines which should happen at all optimization levels.
// (E.g. ones that facilitate matching for the selector) For example, matching
// pseudos.
def AArch64PostLegalizerLoweringHelper
: GICombinerHelper<"AArch64GenPostLegalizerLoweringHelper",
def AArch64PostLegalizerLowering
: GICombinerHelper<"AArch64PostLegalizerLoweringImpl",
[shuffle_vector_lowering, vashr_vlshr_imm,
icmp_lowering, build_vector_lowering,
lower_vector_fcmp, form_truncstore,
vector_sext_inreg_to_shift]> {
let DisableRuleOption = "aarch64postlegalizerlowering-disable-rule";
}

// Post-legalization combines which are primarily optimizations.
def AArch64PostLegalizerCombinerHelper
: GICombinerHelper<"AArch64GenPostLegalizerCombinerHelper",
def AArch64PostLegalizerCombiner
: GICombinerHelper<"AArch64PostLegalizerCombinerImpl",
[copy_prop, combines_for_extload,
sext_trunc_sextload, mutate_anyext_to_zext,
hoist_logic_op_with_same_opcode_hands,
Expand All @@ -238,5 +231,4 @@ def AArch64PostLegalizerCombinerHelper
ptr_add_immed_chain, overlapping_and,
split_store_zero_128, undef_combines,
select_to_minmax]> {
let DisableRuleOption = "aarch64postlegalizercombiner-disable-rule";
}
16 changes: 8 additions & 8 deletions llvm/lib/Target/AArch64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ tablegen(LLVM AArch64GenDAGISel.inc -gen-dag-isel)
tablegen(LLVM AArch64GenDisassemblerTables.inc -gen-disassembler)
tablegen(LLVM AArch64GenFastISel.inc -gen-fast-isel)
tablegen(LLVM AArch64GenGlobalISel.inc -gen-global-isel)
tablegen(LLVM AArch64GenO0PreLegalizeGICombiner.inc -gen-global-isel-combiner
-combiners="AArch64O0PreLegalizerCombinerHelper")
tablegen(LLVM AArch64GenPreLegalizeGICombiner.inc -gen-global-isel-combiner
-combiners="AArch64PreLegalizerCombinerHelper")
tablegen(LLVM AArch64GenPostLegalizeGICombiner.inc -gen-global-isel-combiner
-combiners="AArch64PostLegalizerCombinerHelper")
tablegen(LLVM AArch64GenPostLegalizeGILowering.inc -gen-global-isel-combiner
-combiners="AArch64PostLegalizerLoweringHelper")
tablegen(LLVM AArch64GenO0PreLegalizeGICombiner.inc -gen-global-isel-combiner-matchtable
-combiners="AArch64O0PreLegalizerCombiner")
tablegen(LLVM AArch64GenPreLegalizeGICombiner.inc -gen-global-isel-combiner-matchtable
-combiners="AArch64PreLegalizerCombiner")
tablegen(LLVM AArch64GenPostLegalizeGICombiner.inc -gen-global-isel-combiner-matchtable
-combiners="AArch64PostLegalizerCombiner")
tablegen(LLVM AArch64GenPostLegalizeGILowering.inc -gen-global-isel-combiner-matchtable
-combiners="AArch64PostLegalizerLowering")
tablegen(LLVM AArch64GenInstrInfo.inc -gen-instr-info)
tablegen(LLVM AArch64GenMCCodeEmitter.inc -gen-emitter)
tablegen(LLVM AArch64GenMCPseudoLowering.inc -gen-pseudo-lowering)
Expand Down
67 changes: 50 additions & 17 deletions llvm/lib/Target/AArch64/GISel/AArch64O0PreLegalizerCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "llvm/CodeGen/GlobalISel/Combiner.h"
#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h"
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
#include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
#include "llvm/CodeGen/GlobalISel/MIPatternMatch.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
Expand All @@ -27,33 +29,67 @@
#include "llvm/IR/Instructions.h"
#include "llvm/Support/Debug.h"

#define GET_GICOMBINER_DEPS
#include "AArch64GenO0PreLegalizeGICombiner.inc"
#undef GET_GICOMBINER_DEPS

#define DEBUG_TYPE "aarch64-O0-prelegalizer-combiner"

using namespace llvm;
using namespace MIPatternMatch;
namespace {
#define GET_GICOMBINER_TYPES
#include "AArch64GenO0PreLegalizeGICombiner.inc"
#undef GET_GICOMBINER_TYPES

class AArch64O0PreLegalizerCombinerHelperState {
class AArch64O0PreLegalizerCombinerImpl : public GIMatchTableExecutor {
protected:
CombinerHelper &Helper;
const AArch64O0PreLegalizerCombinerImplRuleConfig &RuleConfig;

const AArch64Subtarget &STI;
GISelChangeObserver &Observer;
MachineIRBuilder &B;
MachineFunction &MF;

MachineRegisterInfo &MRI;

public:
AArch64O0PreLegalizerCombinerHelperState(CombinerHelper &Helper)
: Helper(Helper) {}
};
AArch64O0PreLegalizerCombinerImpl(
const AArch64O0PreLegalizerCombinerImplRuleConfig &RuleConfig,
GISelChangeObserver &Observer, MachineIRBuilder &B,
CombinerHelper &Helper);

static const char *getName() { return "AArch64O0PreLegalizerCombiner"; }

#define AARCH64O0PRELEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_DEPS
bool tryCombineAll(MachineInstr &I) const;

private:
#define GET_GICOMBINER_CLASS_MEMBERS
#include "AArch64GenO0PreLegalizeGICombiner.inc"
#undef AARCH64O0PRELEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_DEPS
#undef GET_GICOMBINER_CLASS_MEMBERS
};

namespace {
#define AARCH64O0PRELEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_H
#define GET_GICOMBINER_IMPL
#include "AArch64GenO0PreLegalizeGICombiner.inc"
#undef GET_GICOMBINER_IMPL

AArch64O0PreLegalizerCombinerImpl::AArch64O0PreLegalizerCombinerImpl(
const AArch64O0PreLegalizerCombinerImplRuleConfig &RuleConfig,
GISelChangeObserver &Observer, MachineIRBuilder &B, CombinerHelper &Helper)
: Helper(Helper), RuleConfig(RuleConfig),
STI(B.getMF().getSubtarget<AArch64Subtarget>()), Observer(Observer), B(B),
MF(B.getMF()), MRI(*B.getMRI()),
#define GET_GICOMBINER_CONSTRUCTOR_INITS
#include "AArch64GenO0PreLegalizeGICombiner.inc"
#undef AARCH64O0PRELEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_H
#undef GET_GICOMBINER_CONSTRUCTOR_INITS
{
}

class AArch64O0PreLegalizerCombinerInfo : public CombinerInfo {
GISelKnownBits *KB;
MachineDominatorTree *MDT;
AArch64GenO0PreLegalizerCombinerHelperRuleConfig GeneratedRuleCfg;
AArch64O0PreLegalizerCombinerImplRuleConfig RuleConfig;

public:
AArch64O0PreLegalizerCombinerInfo(bool EnableOpt, bool OptSize, bool MinSize,
Expand All @@ -62,7 +98,7 @@ class AArch64O0PreLegalizerCombinerInfo : public CombinerInfo {
: CombinerInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
/*LegalizerInfo*/ nullptr, EnableOpt, OptSize, MinSize),
KB(KB), MDT(MDT) {
if (!GeneratedRuleCfg.parseCommandLineOption())
if (!RuleConfig.parseCommandLineOption())
report_fatal_error("Invalid rule identifier");
}

Expand All @@ -74,9 +110,10 @@ bool AArch64O0PreLegalizerCombinerInfo::combine(GISelChangeObserver &Observer,
MachineInstr &MI,
MachineIRBuilder &B) const {
CombinerHelper Helper(Observer, B, /*IsPreLegalize*/ true, KB, MDT);
AArch64GenO0PreLegalizerCombinerHelper Generated(GeneratedRuleCfg, Helper);
AArch64O0PreLegalizerCombinerImpl Impl(RuleConfig, Observer, B, Helper);
Impl.setupMF(*MI.getMF(), KB);

if (Generated.tryCombineAll(Observer, MI, B))
if (Impl.tryCombineAll(MI))
return true;

unsigned Opc = MI.getOpcode();
Expand Down Expand Up @@ -104,10 +141,6 @@ bool AArch64O0PreLegalizerCombinerInfo::combine(GISelChangeObserver &Observer,
return false;
}

#define AARCH64O0PRELEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_CPP
#include "AArch64GenO0PreLegalizeGICombiner.inc"
#undef AARCH64O0PRELEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_CPP

// Pass boilerplate
// ================

Expand Down
93 changes: 68 additions & 25 deletions llvm/lib/Target/AArch64/GISel/AArch64PostLegalizerCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "llvm/CodeGen/GlobalISel/Combiner.h"
#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h"
#include "llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h"
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
#include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
#include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h"
Expand All @@ -37,11 +39,21 @@
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/Support/Debug.h"

#define GET_GICOMBINER_DEPS
#include "AArch64GenPostLegalizeGICombiner.inc"
#undef GET_GICOMBINER_DEPS

#define DEBUG_TYPE "aarch64-postlegalizer-combiner"

using namespace llvm;
using namespace MIPatternMatch;

namespace {

#define GET_GICOMBINER_TYPES
#include "AArch64GenPostLegalizeGICombiner.inc"
#undef GET_GICOMBINER_TYPES

/// This combine tries do what performExtractVectorEltCombine does in SDAG.
/// Rewrite for pairwise fadd pattern
/// (s32 (g_extract_vector_elt
Expand Down Expand Up @@ -109,13 +121,13 @@ void applyExtractVecEltPairwiseAdd(
MI.eraseFromParent();
}

static bool isSignExtended(Register R, MachineRegisterInfo &MRI) {
bool isSignExtended(Register R, MachineRegisterInfo &MRI) {
// TODO: check if extended build vector as well.
unsigned Opc = MRI.getVRegDef(R)->getOpcode();
return Opc == TargetOpcode::G_SEXT || Opc == TargetOpcode::G_SEXT_INREG;
}

static bool isZeroExtended(Register R, MachineRegisterInfo &MRI) {
bool isZeroExtended(Register R, MachineRegisterInfo &MRI) {
// TODO: check if extended build vector as well.
return MRI.getVRegDef(R)->getOpcode() == TargetOpcode::G_ZEXT;
}
Expand Down Expand Up @@ -264,7 +276,7 @@ void applyFoldMergeToZext(MachineInstr &MI, MachineRegisterInfo &MRI,

/// \returns True if a G_ANYEXT instruction \p MI should be mutated to a G_ZEXT
/// instruction.
static bool matchMutateAnyExtToZExt(MachineInstr &MI, MachineRegisterInfo &MRI) {
bool matchMutateAnyExtToZExt(MachineInstr &MI, MachineRegisterInfo &MRI) {
// If this is coming from a scalar compare then we can use a G_ZEXT instead of
// a G_ANYEXT:
//
Expand All @@ -281,17 +293,17 @@ static bool matchMutateAnyExtToZExt(MachineInstr &MI, MachineRegisterInfo &MRI)
m_GFCmp(m_Pred(), m_Reg(), m_Reg())));
}

static void applyMutateAnyExtToZExt(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &B,
GISelChangeObserver &Observer) {
void applyMutateAnyExtToZExt(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &B,
GISelChangeObserver &Observer) {
Observer.changingInstr(MI);
MI.setDesc(B.getTII().get(TargetOpcode::G_ZEXT));
Observer.changedInstr(MI);
}

/// Match a 128b store of zero and split it into two 64 bit stores, for
/// size/performance reasons.
static bool matchSplitStoreZero128(MachineInstr &MI, MachineRegisterInfo &MRI) {
bool matchSplitStoreZero128(MachineInstr &MI, MachineRegisterInfo &MRI) {
GStore &Store = cast<GStore>(MI);
if (!Store.isSimple())
return false;
Expand All @@ -307,9 +319,9 @@ static bool matchSplitStoreZero128(MachineInstr &MI, MachineRegisterInfo &MRI) {
return MaybeCst && MaybeCst->isZero();
}

static void applySplitStoreZero128(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &B,
GISelChangeObserver &Observer) {
void applySplitStoreZero128(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &B,
GISelChangeObserver &Observer) {
B.setInstrAndDebugLoc(MI);
GStore &Store = cast<GStore>(MI);
assert(MRI.getType(Store.getValueReg()).isVector() &&
Expand All @@ -327,29 +339,63 @@ static void applySplitStoreZero128(MachineInstr &MI, MachineRegisterInfo &MRI,
Store.eraseFromParent();
}

#define AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_DEPS
class AArch64PostLegalizerCombinerImpl : public GIMatchTableExecutor {
protected:
CombinerHelper &Helper;
const AArch64PostLegalizerCombinerImplRuleConfig &RuleConfig;

const AArch64Subtarget &STI;
MachineRegisterInfo &MRI;
GISelChangeObserver &Observer;
MachineIRBuilder &B;
MachineFunction &MF;

public:
AArch64PostLegalizerCombinerImpl(
const AArch64PostLegalizerCombinerImplRuleConfig &RuleConfig,
const AArch64Subtarget &STI, GISelChangeObserver &Observer,
MachineIRBuilder &B, CombinerHelper &Helper);

static const char *getName() { return "AArch64PostLegalizerCombiner"; }

bool tryCombineAll(MachineInstr &I) const;

private:
#define GET_GICOMBINER_CLASS_MEMBERS
#include "AArch64GenPostLegalizeGICombiner.inc"
#undef AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_DEPS
#undef GET_GICOMBINER_CLASS_MEMBERS
};

namespace {
#define AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_H
#define GET_GICOMBINER_IMPL
#include "AArch64GenPostLegalizeGICombiner.inc"
#undef AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_H
#undef GET_GICOMBINER_IMPL

AArch64PostLegalizerCombinerImpl::AArch64PostLegalizerCombinerImpl(
const AArch64PostLegalizerCombinerImplRuleConfig &RuleConfig,
const AArch64Subtarget &STI, GISelChangeObserver &Observer,
MachineIRBuilder &B, CombinerHelper &Helper)
: Helper(Helper), RuleConfig(RuleConfig), STI(STI), MRI(*B.getMRI()),
Observer(Observer), B(B), MF(B.getMF()),
#define GET_GICOMBINER_CONSTRUCTOR_INITS
#include "AArch64GenPostLegalizeGICombiner.inc"
#undef GET_GICOMBINER_CONSTRUCTOR_INITS
{
}

class AArch64PostLegalizerCombinerInfo : public CombinerInfo {
GISelKnownBits *KB;
MachineDominatorTree *MDT;

public:
AArch64GenPostLegalizerCombinerHelperRuleConfig GeneratedRuleCfg;
AArch64PostLegalizerCombinerImplRuleConfig RuleConfig;

AArch64PostLegalizerCombinerInfo(bool EnableOpt, bool OptSize, bool MinSize,
GISelKnownBits *KB,
MachineDominatorTree *MDT)
: CombinerInfo(/*AllowIllegalOps*/ true, /*ShouldLegalizeIllegal*/ false,
/*LegalizerInfo*/ nullptr, EnableOpt, OptSize, MinSize),
KB(KB), MDT(MDT) {
if (!GeneratedRuleCfg.parseCommandLineOption())
if (!RuleConfig.parseCommandLineOption())
report_fatal_error("Invalid rule identifier");
}

Expand All @@ -360,17 +406,14 @@ class AArch64PostLegalizerCombinerInfo : public CombinerInfo {
bool AArch64PostLegalizerCombinerInfo::combine(GISelChangeObserver &Observer,
MachineInstr &MI,
MachineIRBuilder &B) const {
const auto *LI =
MI.getParent()->getParent()->getSubtarget().getLegalizerInfo();
const auto &STI = MI.getMF()->getSubtarget<AArch64Subtarget>();
const auto *LI = STI.getLegalizerInfo();
CombinerHelper Helper(Observer, B, /*IsPreLegalize*/ false, KB, MDT, LI);
AArch64GenPostLegalizerCombinerHelper Generated(GeneratedRuleCfg);
return Generated.tryCombineAll(Observer, MI, B, Helper);
AArch64PostLegalizerCombinerImpl Impl(RuleConfig, STI, Observer, B, Helper);
Impl.setupMF(*MI.getMF(), KB);
return Impl.tryCombineAll(MI);
}

#define AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_CPP
#include "AArch64GenPostLegalizeGICombiner.inc"
#undef AARCH64POSTLEGALIZERCOMBINERHELPER_GENCOMBINERHELPER_CPP

class AArch64PostLegalizerCombiner : public MachineFunctionPass {
public:
static char ID;
Expand Down
Loading

0 comments on commit 655714a

Please sign in to comment.