@@ -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-
7165template <typename ConstT>
72- std::optional<ConstT> matchConstant (Register Reg, const MachineRegisterInfo &MRI);
66+ inline std::optional<ConstT> matchConstant (Register,
67+ const MachineRegisterInfo &);
7368
7469template <>
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
8175template <>
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
14881template <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
165100template <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-
220151inline GCstAndRegMatch m_GCst (std::optional<ValueAndVReg> &ValReg) {
221152 return GCstAndRegMatch (ValReg);
222153}
@@ -394,25 +325,34 @@ template <typename BindTy> struct bind_helper {
394325template <> 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
405340template <> 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
412349template <> 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
429369inline bind_ty<Register> m_Reg (Register &R) { return R; }
430370inline 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; }
432372inline bind_ty<CmpInst::Predicate> m_Pred (CmpInst::Predicate &P) { return P; }
433373inline 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
447387inline 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
469390template <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
0 commit comments