Skip to content

Commit 29c7834

Browse files
Sightemadriweb
authored andcommitted
Refactor MIPatternMatch usage to v16
- Revert MIPatternMatch.h to upstream v16.0.6 - Fix CombinerHelper.cpp to use m_ICst(int64_t&) and m_GCst() - Fix z80 backend files to use int64_t for pattern matching - Fix InstSizes.cpp
1 parent 519b26a commit 29c7834

File tree

6 files changed

+73
-131
lines changed

6 files changed

+73
-131
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct PtrAddChain {
7878

7979
struct FunnelShift {
8080
Register ShiftLeftReg, ShiftRightReg;
81-
uint64_t ShiftLeftAmt;
81+
int64_t ShiftLeftAmt;
8282
};
8383

8484
struct RegisterImmPair {

llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h

Lines changed: 39 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -62,87 +62,20 @@ inline OneNonDBGUse_match<SubPat> m_OneNonDBGUse(const SubPat &SP) {
6262
return SP;
6363
}
6464

65-
struct IgnoreMatch {
66-
IgnoreMatch() = default;
67-
IgnoreMatch(const IgnoreMatch &) = default;
68-
const IgnoreMatch &operator=(const IgnoreMatch &) const { return *this; }
69-
};
70-
7165
template <typename ConstT>
72-
std::optional<ConstT> matchConstant(Register Reg, const MachineRegisterInfo &MRI);
66+
inline std::optional<ConstT> matchConstant(Register,
67+
const MachineRegisterInfo &);
7368

7469
template <>
75-
inline std::optional<std::optional<ValueAndVReg>>
76-
matchConstant<std::optional<ValueAndVReg>>(Register Reg,
77-
const MachineRegisterInfo &MRI) {
78-
return getIConstantVRegValWithLookThrough(Reg, MRI);
70+
inline std::optional<APInt> matchConstant(Register Reg,
71+
const MachineRegisterInfo &MRI) {
72+
return getIConstantVRegVal(Reg, MRI);
7973
}
8074

8175
template <>
82-
inline std::optional<APInt> matchConstant<APInt>(Register Reg,
76+
inline std::optional<int64_t> matchConstant(Register Reg,
8377
const MachineRegisterInfo &MRI) {
84-
const auto& Val = matchConstant<std::optional<ValueAndVReg>>(Reg, MRI);
85-
if (Val.has_value() && Val.value().has_value())
86-
return { Val.value()->Value };
87-
return { std::nullopt };
88-
}
89-
90-
template <>
91-
inline std::optional<const IgnoreMatch>
92-
matchConstant<const IgnoreMatch>(Register Reg, const MachineRegisterInfo &MRI) {
93-
matchConstant<APInt>(Reg, MRI);
94-
return std::optional<const IgnoreMatch>{ IgnoreMatch{} };
95-
}
96-
97-
template <>
98-
inline std::optional<int64_t> matchConstant<int64_t>(Register Reg,
99-
const MachineRegisterInfo &MRI) {
100-
auto Val = matchConstant<APInt>(Reg, MRI);
101-
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
102-
return Val->getSExtValue();
103-
return std::nullopt;
104-
}
105-
106-
template <>
107-
inline std::optional<uint64_t> matchConstant<uint64_t>(Register Reg,
108-
const MachineRegisterInfo &MRI) {
109-
auto Val = matchConstant<APInt>(Reg, MRI);
110-
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
111-
return Val->getSExtValue();
112-
return std::nullopt;
113-
}
114-
115-
template <>
116-
inline std::optional<unsigned char> matchConstant<unsigned char>(Register Reg,
117-
const MachineRegisterInfo &MRI) {
118-
auto Val = matchConstant<APInt>(Reg, MRI);
119-
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
120-
return Val->getSExtValue();
121-
return std::nullopt;
122-
}
123-
124-
template <>
125-
inline std::optional<unsigned int> matchConstant<unsigned int>(Register Reg,
126-
const MachineRegisterInfo &MRI) {
127-
auto Val = matchConstant<APInt>(Reg, MRI);
128-
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
129-
return Val->getSExtValue();
130-
return std::nullopt;
131-
}
132-
133-
template <>
134-
inline std::optional<bool> matchConstant<bool>(Register Reg,
135-
const MachineRegisterInfo &MRI) {
136-
auto Val = matchConstant<APInt>(Reg, MRI);
137-
if (Val && Val->getBitWidth() > 0 && Val->getBitWidth() <= 64)
138-
return Val->getSExtValue();
139-
return std::nullopt;
140-
}
141-
142-
template <>
143-
inline std::optional<ValueAndVReg> matchConstant<ValueAndVReg>(Register Reg,
144-
const MachineRegisterInfo &MRI) {
145-
return getIConstantVRegValWithLookThrough(Reg, MRI);
78+
return getIConstantVRegSExtVal(Reg, MRI);
14679
}
14780

14881
template <typename ConstT> struct ConstantMatch {
@@ -157,9 +90,11 @@ template <typename ConstT> struct ConstantMatch {
15790
}
15891
};
15992

160-
inline ConstantMatch<const IgnoreMatch> m_ICst() {
161-
static constexpr IgnoreMatch ignore;
162-
return {ignore};
93+
inline ConstantMatch<APInt> m_ICst(APInt &Cst) {
94+
return ConstantMatch<APInt>(Cst);
95+
}
96+
inline ConstantMatch<int64_t> m_ICst(int64_t &Cst) {
97+
return ConstantMatch<int64_t>(Cst);
16398
}
16499

165100
template <typename ConstT>
@@ -213,10 +148,6 @@ struct GCstAndRegMatch {
213148
}
214149
};
215150

216-
template <typename ConstT> inline ConstantMatch<ConstT> m_ICst(ConstT &Cst) {
217-
return {Cst};
218-
}
219-
220151
inline GCstAndRegMatch m_GCst(std::optional<ValueAndVReg> &ValReg) {
221152
return GCstAndRegMatch(ValReg);
222153
}
@@ -394,25 +325,34 @@ template <typename BindTy> struct bind_helper {
394325
template <> struct bind_helper<MachineInstr *> {
395326
static bool bind(const MachineRegisterInfo &MRI, MachineInstr *&MI,
396327
Register Reg) {
397-
return MI = MRI.getVRegDef(Reg);
328+
MI = MRI.getVRegDef(Reg);
329+
if (MI)
330+
return true;
331+
return false;
398332
}
399333
static bool bind(const MachineRegisterInfo &MRI, MachineInstr *&MI,
400334
MachineInstr *Inst) {
401-
return MI = Inst;
335+
MI = Inst;
336+
return MI;
402337
}
403338
};
404339

405340
template <> struct bind_helper<LLT> {
406-
static bool bind(const MachineRegisterInfo &MRI, LLT &Ty, Register Reg) {
341+
static bool bind(const MachineRegisterInfo &MRI, LLT Ty, Register Reg) {
407342
Ty = MRI.getType(Reg);
408-
return Ty.isValid();
343+
if (Ty.isValid())
344+
return true;
345+
return false;
409346
}
410347
};
411348

412349
template <> struct bind_helper<const ConstantFP *> {
413350
static bool bind(const MachineRegisterInfo &MRI, const ConstantFP *&F,
414351
Register Reg) {
415-
return F = getConstantFPVRegVal(Reg, MRI);
352+
F = getConstantFPVRegVal(Reg, MRI);
353+
if (F)
354+
return true;
355+
return false;
416356
}
417357
};
418358

@@ -428,7 +368,7 @@ template <typename Class> struct bind_ty {
428368

429369
inline bind_ty<Register> m_Reg(Register &R) { return R; }
430370
inline bind_ty<MachineInstr *> m_MInstr(MachineInstr *&MI) { return MI; }
431-
inline bind_ty<LLT> m_Type(LLT &Ty) { return Ty; }
371+
inline bind_ty<LLT> m_Type(LLT Ty) { return Ty; }
432372
inline bind_ty<CmpInst::Predicate> m_Pred(CmpInst::Predicate &P) { return P; }
433373
inline operand_type_match m_Pred() { return operand_type_match(); }
434374

@@ -446,25 +386,6 @@ inline ImplicitDefMatch m_GImplicitDef() { return ImplicitDefMatch(); }
446386
// Helper for matching G_FCONSTANT
447387
inline bind_ty<const ConstantFP *> m_GFCst(const ConstantFP *&C) { return C; }
448388

449-
template <typename Class> struct specific_ty {
450-
Class RequestedVal;
451-
452-
specific_ty(Class RequestedVal) : RequestedVal(RequestedVal) {}
453-
454-
bool match(const MachineRegisterInfo &MRI, Register Reg) {
455-
Class MatchedVal;
456-
return mi_match(Reg, MRI, bind_ty<Class>(MatchedVal)) &&
457-
MatchedVal == RequestedVal;
458-
}
459-
};
460-
461-
inline specific_ty<MachineInstr *> m_SpecificMInstr(MachineInstr *MI) {
462-
return MI;
463-
}
464-
inline specific_ty<CmpInst::Predicate> m_SpecificPred(CmpInst::Predicate P) {
465-
return P;
466-
}
467-
468389
// General helper for all the binary generic MI such as G_ADD/G_SUB etc
469390
template <typename LHS_P, typename RHS_P, unsigned Opcode,
470391
bool Commutable = false>
@@ -733,13 +654,18 @@ struct CompareOp_match {
733654
if (!mi_match(Op, MRI, m_MInstr(TmpMI)) || TmpMI->getOpcode() != Opcode)
734655
return false;
735656

736-
auto TmpPred = CmpInst::Predicate(TmpMI->getOperand(1).getPredicate());
737-
return (P.match(MRI, TmpPred) &&
738-
L.match(MRI, TmpMI->getOperand(2).getReg()) &&
739-
R.match(MRI, TmpMI->getOperand(3).getReg())) ||
740-
(P.match(MRI, CmpInst::getSwappedPredicate(TmpPred)) &&
741-
R.match(MRI, TmpMI->getOperand(2).getReg()) &&
742-
L.match(MRI, TmpMI->getOperand(3).getReg()));
657+
auto TmpPred =
658+
static_cast<CmpInst::Predicate>(TmpMI->getOperand(1).getPredicate());
659+
if (!P.match(MRI, TmpPred))
660+
return false;
661+
Register LHS = TmpMI->getOperand(2).getReg();
662+
Register RHS = TmpMI->getOperand(3).getReg();
663+
if (L.match(MRI, LHS) && R.match(MRI, RHS))
664+
return true;
665+
if (Commutable && L.match(MRI, RHS) && R.match(MRI, LHS) &&
666+
P.match(MRI, CmpInst::getSwappedPredicate(TmpPred)))
667+
return true;
668+
return false;
743669
}
744670
};
745671

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,10 @@ void CombinerHelper::applySextInRegOfLoad(
907907
MI.eraseFromParent();
908908
}
909909

910+
// Z80-FORK-START: CUSTOM-HELPERS
911+
// helper functions not found in upstream v17
912+
// this block should be kept when rebasing to 17+, check for conflicts
913+
910914
bool CombinerHelper::dominates(MachineBasicBlock &DefMBB,
911915
MachineBasicBlock &UseMBB) {
912916
if (MDT)
@@ -938,6 +942,8 @@ bool CombinerHelper::canMove(MachineInstr &MI, MachineBasicBlock &MBB,
938942
return true;
939943
}
940944

945+
// Z80-FORK-END: CUSTOM-HELPERS
946+
941947
bool CombinerHelper::findPostIndexCandidate(MachineInstr &MI, Register &Addr,
942948
Register &Base, Register &Offset) {
943949
auto &MF = *MI.getParent()->getParent();
@@ -4661,9 +4667,9 @@ bool CombinerHelper::matchReassocConstantInnerLHS(GPtrAdd &MI,
46614667
// G_PTR_ADD (G_PTR_ADD X, C), Y) -> (G_PTR_ADD (G_PTR_ADD(X, Y), C)
46624668
// if and only if (G_PTR_ADD X, C) has one use.
46634669
Register LHSBase;
4664-
ValueAndVReg LHSCstOff;
4670+
std::optional<ValueAndVReg> LHSCstOff;
46654671
if (!mi_match(MI.getBaseReg(), MRI,
4666-
m_OneNonDBGUse(m_GPtrAdd(m_Reg(LHSBase), m_ICst(LHSCstOff)))))
4672+
m_OneNonDBGUse(m_GPtrAdd(m_Reg(LHSBase), m_GCst(LHSCstOff)))))
46674673
return false;
46684674

46694675
auto *LHSPtrAdd = cast<GPtrAdd>(LHS);
@@ -4674,7 +4680,7 @@ bool CombinerHelper::matchReassocConstantInnerLHS(GPtrAdd &MI,
46744680
LHSPtrAdd->moveBefore(&MI);
46754681
Register RHSReg = MI.getOffsetReg();
46764682
// set VReg will cause type mismatch if it comes from extend/trunc
4677-
auto NewCst = B.buildConstant(MRI.getType(RHSReg), LHSCstOff.Value);
4683+
auto NewCst = B.buildConstant(MRI.getType(RHSReg), LHSCstOff->Value);
46784684
Observer.changingInstr(MI);
46794685
MI.getOperand(2).setReg(NewCst.getReg(0));
46804686
Observer.changedInstr(MI);
@@ -6236,6 +6242,11 @@ bool CombinerHelper::matchRedundantBinOpInEquality(MachineInstr &MI,
62366242
return CmpInst::isEquality(Pred) && Y.isValid();
62376243
}
62386244

6245+
// Z80-FORK-START: CUSTOM-COMBINERS
6246+
// These are Z80-specific generic improvements not found in upstream v17.
6247+
// WHEN REBASING TO v17+: Keep this block, but check for conflicts.
6248+
// Consider upstreaming these improvements to LLVM.
6249+
62396250
bool CombinerHelper::matchPtrAddGlobalImmed(
62406251
MachineInstr &MI, std::pair<const GlobalValue *, int64_t> &MatchInfo) {
62416252
// We're trying to match the following pattern:
@@ -6354,12 +6365,15 @@ bool CombinerHelper::matchReassocFoldConstants(MachineInstr &MI,
63546365
Opc == TargetOpcode::G_XOR) &&
63556366
"Expected associative opcode");
63566367

6368+
// Z80-FORK-START: matchReassocFoldConstants pattern matching
6369+
int64_t _Unused1, _Unused2; // dummy vars for upstream api
63576370
return mi_match(
63586371
MI, MRI,
63596372
m_CommutativeBinOp(Opc,
63606373
m_OneNonDBGUse(m_CommutativeBinOp(
6361-
Opc, m_Reg(), m_all_of(m_ICst(), m_Reg(Regs[0])))),
6362-
m_all_of(m_ICst(), m_Reg(Regs[1]))));
6374+
Opc, m_Reg(), m_all_of(m_ICst(_Unused1), m_Reg(Regs[0])))),
6375+
m_all_of(m_ICst(_Unused2), m_Reg(Regs[1]))));
6376+
// Z80-FORK-END
63636377
}
63646378

63656379
void CombinerHelper::applyReassocFoldConstants(MachineInstr &MI,
@@ -6484,13 +6498,13 @@ bool CombinerHelper::applyCombineOrToAdd(MachineInstr &MI) {
64846498
bool CombinerHelper::matchCombineFunnelShift(MachineInstr &MI,
64856499
FunnelShift &MatchInfo) {
64866500
Register DstReg = MI.getOperand(0).getReg();
6487-
uint64_t ShiftRightAmt;
6501+
int64_t ShiftRightAmt;
64886502
return mi_match(DstReg, MRI,
64896503
m_GAdd(m_GShl(m_Reg(MatchInfo.ShiftLeftReg),
64906504
m_ICst(MatchInfo.ShiftLeftAmt)),
64916505
m_GLShr(m_Reg(MatchInfo.ShiftRightReg),
64926506
m_ICst(ShiftRightAmt)))) &&
6493-
MatchInfo.ShiftLeftAmt + ShiftRightAmt ==
6507+
static_cast<uint64_t>(MatchInfo.ShiftLeftAmt) + static_cast<uint64_t>(ShiftRightAmt) ==
64946508
MRI.getType(DstReg).getSizeInBits();
64956509
}
64966510

@@ -6903,7 +6917,7 @@ void CombinerHelper::applyNarrowICmp(MachineInstr &MI,
69036917
bool CombinerHelper::matchSimplifyICmpBool(MachineInstr &MI,
69046918
RegisterImmPair &MatchInfo) {
69056919
CmpInst::Predicate Pred;
6906-
bool CmpVal;
6920+
int64_t CmpVal; // Z80-FORK: changed from bool to int64_t for upstream api
69076921
if (!mi_match(MI.getOperand(0).getReg(), MRI,
69086922
m_GICmp(m_Pred(Pred), m_Reg(MatchInfo.Reg), m_ICst(CmpVal))) ||
69096923
MRI.getType(MatchInfo.Reg) != LLT::scalar(1))
@@ -6912,9 +6926,10 @@ bool CombinerHelper::matchSimplifyICmpBool(MachineInstr &MI,
69126926
LLVMContext &C = MI.getParent()->getParent()->getFunction().getContext();
69136927
MatchInfo.Imm = 0;
69146928
ConstantInt *BoolCI[2] = {ConstantInt::getFalse(C), ConstantInt::getTrue(C)};
6929+
bool CmpBool = (CmpVal != 0); // Z80-FORK: convert back to bool for indexing
69156930
for (ConstantInt *InCI : BoolCI)
69166931
if (Constant *ResC =
6917-
ConstantExpr::getCompare(Pred, InCI, BoolCI[CmpVal], true))
6932+
ConstantExpr::getCompare(Pred, InCI, BoolCI[CmpBool], true))
69186933
MatchInfo.Imm = MatchInfo.Imm << 1 | (ResC == BoolCI[true]);
69196934
else
69206935
return false;
@@ -7139,6 +7154,8 @@ void CombinerHelper::applySinkConstant(MachineInstr &MI,
71397154
DomUseMI.getParent()->insert(DomUseMI, &MI);
71407155
}
71417156

7157+
// Z80-FORK-END: CUSTOM-COMBINERS
7158+
71427159
bool CombinerHelper::tryCombine(MachineInstr &MI) {
71437160
if (tryCombineCopy(MI))
71447161
return true;

llvm/lib/Target/Z80/GISel/Z80InstructionSelector.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,13 +1447,13 @@ Z80InstructionSelector::foldCompare(MachineInstr &I, MachineIRBuilder &MIB,
14471447
if (!ConstRHS->Value && (CC == Z80::COND_Z || CC == Z80::COND_NZ)) {
14481448
if (OpSize == 8) {
14491449
Register SrcReg;
1450-
uint8_t Mask;
1450+
int64_t Mask;
14511451
if (mi_match(LHSReg, MRI,
14521452
m_OneUse(m_GAnd(m_Reg(SrcReg), m_ICst(Mask)))) &&
1453-
isPowerOf2_32(Mask)) {
1453+
Mask > 0 && isPowerOf2_64(static_cast<uint64_t>(Mask))) {
14541454
Opc = Z80::BIT8gb;
14551455
Reg = {};
1456-
Ops = {SrcReg, uint64_t(findFirstSet(Mask))};
1456+
Ops = {SrcReg, uint64_t(findFirstSet(static_cast<uint64_t>(Mask)))};
14571457
} else {
14581458
Opc = Z80::OR8ar;
14591459
Ops = {Reg};

llvm/lib/Target/Z80/GISel/Z80PreLegalizerCombiner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static bool matchCombineTruncShift(MachineInstr &MI, MachineRegisterInfo &MRI,
7272
LLT DstTy = MRI.getType(DstReg);
7373
if (DstTy != LLT::scalar(8))
7474
return false;
75-
unsigned ShiftAmt;
75+
int64_t ShiftAmt;
7676
if (!mi_match(DstReg, MRI,
7777
m_GTrunc(m_GLShr(m_Reg(SrcReg), m_ICst(ShiftAmt)))) ||
7878
ShiftAmt != 8)
@@ -93,7 +93,7 @@ static void applyCombineTruncShift(MachineInstr &MI, MachineIRBuilder &Builder,
9393

9494
static bool matchFlipSetCCCond(MachineInstr &MI, MachineRegisterInfo &MRI,
9595
MachineInstr *&SetCCMI) {
96-
bool Imm;
96+
int64_t Imm;
9797
return mi_match(MI.getOperand(0).getReg(), MRI,
9898
m_GXor(m_OneUse(m_MInstr(SetCCMI)), m_ICst(Imm))) &&
9999
SetCCMI->getOpcode() == Z80::SetCC && Imm;

0 commit comments

Comments
 (0)