1 //==--------------- llvm/CodeGen/SDPatternMatch.h ---------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// Contains matchers for matching SelectionDAG nodes and values.
10 ///
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CODEGEN_SDPATTERNMATCH_H
14 #define LLVM_CODEGEN_SDPATTERNMATCH_H
15
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/CodeGen/SelectionDAGNodes.h"
20 #include "llvm/CodeGen/TargetLowering.h"
21
22 namespace llvm {
23 namespace SDPatternMatch {
24
25 /// MatchContext can repurpose existing patterns to behave differently under
26 /// a certain context. For instance, `m_Opc(ISD::ADD)` matches plain ADD nodes
27 /// in normal circumstances, but matches VP_ADD nodes under a custom
28 /// VPMatchContext. This design is meant to facilitate code / pattern reusing.
29 class BasicMatchContext {
30 const SelectionDAG *DAG;
31 const TargetLowering *TLI;
32
33 public:
BasicMatchContext(const SelectionDAG * DAG)34 explicit BasicMatchContext(const SelectionDAG *DAG)
35 : DAG(DAG), TLI(DAG ? &DAG->getTargetLoweringInfo() : nullptr) {}
36
BasicMatchContext(const TargetLowering * TLI)37 explicit BasicMatchContext(const TargetLowering *TLI)
38 : DAG(nullptr), TLI(TLI) {}
39
40 // A valid MatchContext has to implement the following functions.
41
getDAG()42 const SelectionDAG *getDAG() const { return DAG; }
43
getTLI()44 const TargetLowering *getTLI() const { return TLI; }
45
46 /// Return true if N effectively has opcode Opcode.
match(SDValue N,unsigned Opcode)47 bool match(SDValue N, unsigned Opcode) const {
48 return N->getOpcode() == Opcode;
49 }
50 };
51
52 template <typename Pattern, typename MatchContext>
sd_context_match(SDValue N,const MatchContext & Ctx,Pattern && P)53 [[nodiscard]] bool sd_context_match(SDValue N, const MatchContext &Ctx,
54 Pattern &&P) {
55 return P.match(Ctx, N);
56 }
57
58 template <typename Pattern, typename MatchContext>
sd_context_match(SDNode * N,const MatchContext & Ctx,Pattern && P)59 [[nodiscard]] bool sd_context_match(SDNode *N, const MatchContext &Ctx,
60 Pattern &&P) {
61 return sd_context_match(SDValue(N, 0), Ctx, P);
62 }
63
64 template <typename Pattern>
sd_match(SDNode * N,const SelectionDAG * DAG,Pattern && P)65 [[nodiscard]] bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P) {
66 return sd_context_match(N, BasicMatchContext(DAG), P);
67 }
68
69 template <typename Pattern>
sd_match(SDValue N,const SelectionDAG * DAG,Pattern && P)70 [[nodiscard]] bool sd_match(SDValue N, const SelectionDAG *DAG, Pattern &&P) {
71 return sd_context_match(N, BasicMatchContext(DAG), P);
72 }
73
74 template <typename Pattern>
sd_match(SDNode * N,Pattern && P)75 [[nodiscard]] bool sd_match(SDNode *N, Pattern &&P) {
76 return sd_match(N, nullptr, P);
77 }
78
79 template <typename Pattern>
sd_match(SDValue N,Pattern && P)80 [[nodiscard]] bool sd_match(SDValue N, Pattern &&P) {
81 return sd_match(N, nullptr, P);
82 }
83
84 // === Utilities ===
85 struct Value_match {
86 SDValue MatchVal;
87
88 Value_match() = default;
89
Value_matchValue_match90 explicit Value_match(SDValue Match) : MatchVal(Match) {}
91
matchValue_match92 template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
93 if (MatchVal)
94 return MatchVal == N;
95 return N.getNode();
96 }
97 };
98
99 /// Match any valid SDValue.
m_Value()100 inline Value_match m_Value() { return Value_match(); }
101
m_Specific(SDValue N)102 inline Value_match m_Specific(SDValue N) {
103 assert(N);
104 return Value_match(N);
105 }
106
107 struct DeferredValue_match {
108 SDValue &MatchVal;
109
DeferredValue_matchDeferredValue_match110 explicit DeferredValue_match(SDValue &Match) : MatchVal(Match) {}
111
matchDeferredValue_match112 template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
113 return N == MatchVal;
114 }
115 };
116
117 /// Similar to m_Specific, but the specific value to match is determined by
118 /// another sub-pattern in the same sd_match() expression. For instance,
119 /// We cannot match `(add V, V)` with `m_Add(m_Value(X), m_Specific(X))` since
120 /// `X` is not initialized at the time it got copied into `m_Specific`. Instead,
121 /// we should use `m_Add(m_Value(X), m_Deferred(X))`.
m_Deferred(SDValue & V)122 inline DeferredValue_match m_Deferred(SDValue &V) {
123 return DeferredValue_match(V);
124 }
125
126 struct Opcode_match {
127 unsigned Opcode;
128
Opcode_matchOpcode_match129 explicit Opcode_match(unsigned Opc) : Opcode(Opc) {}
130
131 template <typename MatchContext>
matchOpcode_match132 bool match(const MatchContext &Ctx, SDValue N) {
133 return Ctx.match(N, Opcode);
134 }
135 };
136
m_Opc(unsigned Opcode)137 inline Opcode_match m_Opc(unsigned Opcode) { return Opcode_match(Opcode); }
138
139 template <unsigned NumUses, typename Pattern> struct NUses_match {
140 Pattern P;
141
NUses_matchNUses_match142 explicit NUses_match(const Pattern &P) : P(P) {}
143
144 template <typename MatchContext>
matchNUses_match145 bool match(const MatchContext &Ctx, SDValue N) {
146 // SDNode::hasNUsesOfValue is pretty expensive when the SDNode produces
147 // multiple results, hence we check the subsequent pattern here before
148 // checking the number of value users.
149 return P.match(Ctx, N) && N->hasNUsesOfValue(NumUses, N.getResNo());
150 }
151 };
152
153 template <typename Pattern>
m_OneUse(const Pattern & P)154 inline NUses_match<1, Pattern> m_OneUse(const Pattern &P) {
155 return NUses_match<1, Pattern>(P);
156 }
157 template <unsigned N, typename Pattern>
m_NUses(const Pattern & P)158 inline NUses_match<N, Pattern> m_NUses(const Pattern &P) {
159 return NUses_match<N, Pattern>(P);
160 }
161
m_OneUse()162 inline NUses_match<1, Value_match> m_OneUse() {
163 return NUses_match<1, Value_match>(m_Value());
164 }
m_NUses()165 template <unsigned N> inline NUses_match<N, Value_match> m_NUses() {
166 return NUses_match<N, Value_match>(m_Value());
167 }
168
169 struct Value_bind {
170 SDValue &BindVal;
171
Value_bindValue_bind172 explicit Value_bind(SDValue &N) : BindVal(N) {}
173
matchValue_bind174 template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
175 BindVal = N;
176 return true;
177 }
178 };
179
m_Value(SDValue & N)180 inline Value_bind m_Value(SDValue &N) { return Value_bind(N); }
181
182 template <typename Pattern, typename PredFuncT> struct TLI_pred_match {
183 Pattern P;
184 PredFuncT PredFunc;
185
TLI_pred_matchTLI_pred_match186 TLI_pred_match(const PredFuncT &Pred, const Pattern &P)
187 : P(P), PredFunc(Pred) {}
188
189 template <typename MatchContext>
matchTLI_pred_match190 bool match(const MatchContext &Ctx, SDValue N) {
191 assert(Ctx.getTLI() && "TargetLowering is required for this pattern.");
192 return PredFunc(*Ctx.getTLI(), N) && P.match(Ctx, N);
193 }
194 };
195
196 // Explicit deduction guide.
197 template <typename PredFuncT, typename Pattern>
198 TLI_pred_match(const PredFuncT &Pred, const Pattern &P)
199 -> TLI_pred_match<Pattern, PredFuncT>;
200
201 /// Match legal SDNodes based on the information provided by TargetLowering.
m_LegalOp(const Pattern & P)202 template <typename Pattern> inline auto m_LegalOp(const Pattern &P) {
203 return TLI_pred_match{[](const TargetLowering &TLI, SDValue N) {
204 return TLI.isOperationLegal(N->getOpcode(),
205 N.getValueType());
206 },
207 P};
208 }
209
210 /// Switch to a different MatchContext for subsequent patterns.
211 template <typename NewMatchContext, typename Pattern> struct SwitchContext {
212 const NewMatchContext &Ctx;
213 Pattern P;
214
215 template <typename OrigMatchContext>
matchSwitchContext216 bool match(const OrigMatchContext &, SDValue N) {
217 return P.match(Ctx, N);
218 }
219 };
220
221 template <typename MatchContext, typename Pattern>
m_Context(const MatchContext & Ctx,Pattern && P)222 inline SwitchContext<MatchContext, Pattern> m_Context(const MatchContext &Ctx,
223 Pattern &&P) {
224 return SwitchContext<MatchContext, Pattern>{Ctx, std::move(P)};
225 }
226
227 // === Value type ===
228 struct ValueType_bind {
229 EVT &BindVT;
230
ValueType_bindValueType_bind231 explicit ValueType_bind(EVT &Bind) : BindVT(Bind) {}
232
matchValueType_bind233 template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
234 BindVT = N.getValueType();
235 return true;
236 }
237 };
238
239 /// Retreive the ValueType of the current SDValue.
m_VT(EVT & VT)240 inline ValueType_bind m_VT(EVT &VT) { return ValueType_bind(VT); }
241
242 template <typename Pattern, typename PredFuncT> struct ValueType_match {
243 PredFuncT PredFunc;
244 Pattern P;
245
ValueType_matchValueType_match246 ValueType_match(const PredFuncT &Pred, const Pattern &P)
247 : PredFunc(Pred), P(P) {}
248
249 template <typename MatchContext>
matchValueType_match250 bool match(const MatchContext &Ctx, SDValue N) {
251 return PredFunc(N.getValueType()) && P.match(Ctx, N);
252 }
253 };
254
255 // Explicit deduction guide.
256 template <typename PredFuncT, typename Pattern>
257 ValueType_match(const PredFuncT &Pred, const Pattern &P)
258 -> ValueType_match<Pattern, PredFuncT>;
259
260 /// Match a specific ValueType.
261 template <typename Pattern>
m_SpecificVT(EVT RefVT,const Pattern & P)262 inline auto m_SpecificVT(EVT RefVT, const Pattern &P) {
263 return ValueType_match{[=](EVT VT) { return VT == RefVT; }, P};
264 }
m_SpecificVT(EVT RefVT)265 inline auto m_SpecificVT(EVT RefVT) {
266 return ValueType_match{[=](EVT VT) { return VT == RefVT; }, m_Value()};
267 }
268
m_Glue()269 inline auto m_Glue() { return m_SpecificVT(MVT::Glue); }
m_OtherVT()270 inline auto m_OtherVT() { return m_SpecificVT(MVT::Other); }
271
272 /// Match any integer ValueTypes.
m_IntegerVT(const Pattern & P)273 template <typename Pattern> inline auto m_IntegerVT(const Pattern &P) {
274 return ValueType_match{[](EVT VT) { return VT.isInteger(); }, P};
275 }
m_IntegerVT()276 inline auto m_IntegerVT() {
277 return ValueType_match{[](EVT VT) { return VT.isInteger(); }, m_Value()};
278 }
279
280 /// Match any floating point ValueTypes.
m_FloatingPointVT(const Pattern & P)281 template <typename Pattern> inline auto m_FloatingPointVT(const Pattern &P) {
282 return ValueType_match{[](EVT VT) { return VT.isFloatingPoint(); }, P};
283 }
m_FloatingPointVT()284 inline auto m_FloatingPointVT() {
285 return ValueType_match{[](EVT VT) { return VT.isFloatingPoint(); },
286 m_Value()};
287 }
288
289 /// Match any vector ValueTypes.
m_VectorVT(const Pattern & P)290 template <typename Pattern> inline auto m_VectorVT(const Pattern &P) {
291 return ValueType_match{[](EVT VT) { return VT.isVector(); }, P};
292 }
m_VectorVT()293 inline auto m_VectorVT() {
294 return ValueType_match{[](EVT VT) { return VT.isVector(); }, m_Value()};
295 }
296
297 /// Match fixed-length vector ValueTypes.
m_FixedVectorVT(const Pattern & P)298 template <typename Pattern> inline auto m_FixedVectorVT(const Pattern &P) {
299 return ValueType_match{[](EVT VT) { return VT.isFixedLengthVector(); }, P};
300 }
m_FixedVectorVT()301 inline auto m_FixedVectorVT() {
302 return ValueType_match{[](EVT VT) { return VT.isFixedLengthVector(); },
303 m_Value()};
304 }
305
306 /// Match scalable vector ValueTypes.
m_ScalableVectorVT(const Pattern & P)307 template <typename Pattern> inline auto m_ScalableVectorVT(const Pattern &P) {
308 return ValueType_match{[](EVT VT) { return VT.isScalableVector(); }, P};
309 }
m_ScalableVectorVT()310 inline auto m_ScalableVectorVT() {
311 return ValueType_match{[](EVT VT) { return VT.isScalableVector(); },
312 m_Value()};
313 }
314
315 /// Match legal ValueTypes based on the information provided by TargetLowering.
m_LegalType(const Pattern & P)316 template <typename Pattern> inline auto m_LegalType(const Pattern &P) {
317 return TLI_pred_match{[](const TargetLowering &TLI, SDValue N) {
318 return TLI.isTypeLegal(N.getValueType());
319 },
320 P};
321 }
322
323 // === Patterns combinators ===
324 template <typename... Preds> struct And {
matchAnd325 template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
326 return true;
327 }
328 };
329
330 template <typename Pred, typename... Preds>
331 struct And<Pred, Preds...> : And<Preds...> {
332 Pred P;
333 And(Pred &&p, Preds &&...preds)
334 : And<Preds...>(std::forward<Preds>(preds)...), P(std::forward<Pred>(p)) {
335 }
336
337 template <typename MatchContext>
338 bool match(const MatchContext &Ctx, SDValue N) {
339 return P.match(Ctx, N) && And<Preds...>::match(Ctx, N);
340 }
341 };
342
343 template <typename... Preds> struct Or {
344 template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
345 return false;
346 }
347 };
348
349 template <typename Pred, typename... Preds>
350 struct Or<Pred, Preds...> : Or<Preds...> {
351 Pred P;
352 Or(Pred &&p, Preds &&...preds)
353 : Or<Preds...>(std::forward<Preds>(preds)...), P(std::forward<Pred>(p)) {}
354
355 template <typename MatchContext>
356 bool match(const MatchContext &Ctx, SDValue N) {
357 return P.match(Ctx, N) || Or<Preds...>::match(Ctx, N);
358 }
359 };
360
361 template <typename... Preds> And<Preds...> m_AllOf(Preds &&...preds) {
362 return And<Preds...>(std::forward<Preds>(preds)...);
363 }
364
365 template <typename... Preds> Or<Preds...> m_AnyOf(Preds &&...preds) {
366 return Or<Preds...>(std::forward<Preds>(preds)...);
367 }
368
369 // === Generic node matching ===
370 template <unsigned OpIdx, typename... OpndPreds> struct Operands_match {
371 template <typename MatchContext>
372 bool match(const MatchContext &Ctx, SDValue N) {
373 // Returns false if there are more operands than predicates;
374 return N->getNumOperands() == OpIdx;
375 }
376 };
377
378 template <unsigned OpIdx, typename OpndPred, typename... OpndPreds>
379 struct Operands_match<OpIdx, OpndPred, OpndPreds...>
380 : Operands_match<OpIdx + 1, OpndPreds...> {
381 OpndPred P;
382
383 Operands_match(OpndPred &&p, OpndPreds &&...preds)
384 : Operands_match<OpIdx + 1, OpndPreds...>(
385 std::forward<OpndPreds>(preds)...),
386 P(std::forward<OpndPred>(p)) {}
387
388 template <typename MatchContext>
389 bool match(const MatchContext &Ctx, SDValue N) {
390 if (OpIdx < N->getNumOperands())
391 return P.match(Ctx, N->getOperand(OpIdx)) &&
392 Operands_match<OpIdx + 1, OpndPreds...>::match(Ctx, N);
393
394 // This is the case where there are more predicates than operands.
395 return false;
396 }
397 };
398
399 template <typename... OpndPreds>
400 auto m_Node(unsigned Opcode, OpndPreds &&...preds) {
401 return m_AllOf(m_Opc(Opcode), Operands_match<0, OpndPreds...>(
402 std::forward<OpndPreds>(preds)...));
403 }
404
405 /// Provide number of operands that are not chain or glue, as well as the first
406 /// index of such operand.
407 template <bool ExcludeChain> struct EffectiveOperands {
408 unsigned Size = 0;
409 unsigned FirstIndex = 0;
410
411 explicit EffectiveOperands(SDValue N) {
412 const unsigned TotalNumOps = N->getNumOperands();
413 FirstIndex = TotalNumOps;
414 for (unsigned I = 0; I < TotalNumOps; ++I) {
415 // Count the number of non-chain and non-glue nodes (we ignore chain
416 // and glue by default) and retreive the operand index offset.
417 EVT VT = N->getOperand(I).getValueType();
418 if (VT != MVT::Glue && VT != MVT::Other) {
419 ++Size;
420 if (FirstIndex == TotalNumOps)
421 FirstIndex = I;
422 }
423 }
424 }
425 };
426
427 template <> struct EffectiveOperands<false> {
428 unsigned Size = 0;
429 unsigned FirstIndex = 0;
430
431 explicit EffectiveOperands(SDValue N) : Size(N->getNumOperands()) {}
432 };
433
434 // === Binary operations ===
435 template <typename LHS_P, typename RHS_P, bool Commutable = false,
436 bool ExcludeChain = false>
437 struct BinaryOpc_match {
438 unsigned Opcode;
439 LHS_P LHS;
440 RHS_P RHS;
441
442 BinaryOpc_match(unsigned Opc, const LHS_P &L, const RHS_P &R)
443 : Opcode(Opc), LHS(L), RHS(R) {}
444
445 template <typename MatchContext>
446 bool match(const MatchContext &Ctx, SDValue N) {
447 if (sd_context_match(N, Ctx, m_Opc(Opcode))) {
448 EffectiveOperands<ExcludeChain> EO(N);
449 assert(EO.Size == 2);
450 return (LHS.match(Ctx, N->getOperand(EO.FirstIndex)) &&
451 RHS.match(Ctx, N->getOperand(EO.FirstIndex + 1))) ||
452 (Commutable && LHS.match(Ctx, N->getOperand(EO.FirstIndex + 1)) &&
453 RHS.match(Ctx, N->getOperand(EO.FirstIndex)));
454 }
455
456 return false;
457 }
458 };
459
460 template <typename LHS, typename RHS>
461 inline BinaryOpc_match<LHS, RHS, false> m_BinOp(unsigned Opc, const LHS &L,
462 const RHS &R) {
463 return BinaryOpc_match<LHS, RHS, false>(Opc, L, R);
464 }
465 template <typename LHS, typename RHS>
466 inline BinaryOpc_match<LHS, RHS, true> m_c_BinOp(unsigned Opc, const LHS &L,
467 const RHS &R) {
468 return BinaryOpc_match<LHS, RHS, true>(Opc, L, R);
469 }
470
471 template <typename LHS, typename RHS>
472 inline BinaryOpc_match<LHS, RHS, false, true>
473 m_ChainedBinOp(unsigned Opc, const LHS &L, const RHS &R) {
474 return BinaryOpc_match<LHS, RHS, false, true>(Opc, L, R);
475 }
476 template <typename LHS, typename RHS>
477 inline BinaryOpc_match<LHS, RHS, true, true>
478 m_c_ChainedBinOp(unsigned Opc, const LHS &L, const RHS &R) {
479 return BinaryOpc_match<LHS, RHS, true, true>(Opc, L, R);
480 }
481
482 // Common binary operations
483 template <typename LHS, typename RHS>
484 inline BinaryOpc_match<LHS, RHS, true> m_Add(const LHS &L, const RHS &R) {
485 return BinaryOpc_match<LHS, RHS, true>(ISD::ADD, L, R);
486 }
487
488 template <typename LHS, typename RHS>
489 inline BinaryOpc_match<LHS, RHS, false> m_Sub(const LHS &L, const RHS &R) {
490 return BinaryOpc_match<LHS, RHS, false>(ISD::SUB, L, R);
491 }
492
493 template <typename LHS, typename RHS>
494 inline BinaryOpc_match<LHS, RHS, true> m_Mul(const LHS &L, const RHS &R) {
495 return BinaryOpc_match<LHS, RHS, true>(ISD::MUL, L, R);
496 }
497
498 template <typename LHS, typename RHS>
499 inline BinaryOpc_match<LHS, RHS, true> m_And(const LHS &L, const RHS &R) {
500 return BinaryOpc_match<LHS, RHS, true>(ISD::AND, L, R);
501 }
502
503 template <typename LHS, typename RHS>
504 inline BinaryOpc_match<LHS, RHS, true> m_Or(const LHS &L, const RHS &R) {
505 return BinaryOpc_match<LHS, RHS, true>(ISD::OR, L, R);
506 }
507
508 template <typename LHS, typename RHS>
509 inline BinaryOpc_match<LHS, RHS, true> m_Xor(const LHS &L, const RHS &R) {
510 return BinaryOpc_match<LHS, RHS, true>(ISD::XOR, L, R);
511 }
512
513 template <typename LHS, typename RHS>
514 inline BinaryOpc_match<LHS, RHS, false> m_UDiv(const LHS &L, const RHS &R) {
515 return BinaryOpc_match<LHS, RHS, false>(ISD::UDIV, L, R);
516 }
517 template <typename LHS, typename RHS>
518 inline BinaryOpc_match<LHS, RHS, false> m_SDiv(const LHS &L, const RHS &R) {
519 return BinaryOpc_match<LHS, RHS, false>(ISD::SDIV, L, R);
520 }
521
522 template <typename LHS, typename RHS>
523 inline BinaryOpc_match<LHS, RHS, false> m_URem(const LHS &L, const RHS &R) {
524 return BinaryOpc_match<LHS, RHS, false>(ISD::UREM, L, R);
525 }
526 template <typename LHS, typename RHS>
527 inline BinaryOpc_match<LHS, RHS, false> m_SRem(const LHS &L, const RHS &R) {
528 return BinaryOpc_match<LHS, RHS, false>(ISD::SREM, L, R);
529 }
530
531 template <typename LHS, typename RHS>
532 inline BinaryOpc_match<LHS, RHS, false> m_Shl(const LHS &L, const RHS &R) {
533 return BinaryOpc_match<LHS, RHS, false>(ISD::SHL, L, R);
534 }
535
536 template <typename LHS, typename RHS>
537 inline BinaryOpc_match<LHS, RHS, false> m_Sra(const LHS &L, const RHS &R) {
538 return BinaryOpc_match<LHS, RHS, false>(ISD::SRA, L, R);
539 }
540 template <typename LHS, typename RHS>
541 inline BinaryOpc_match<LHS, RHS, false> m_Srl(const LHS &L, const RHS &R) {
542 return BinaryOpc_match<LHS, RHS, false>(ISD::SRL, L, R);
543 }
544
545 template <typename LHS, typename RHS>
546 inline BinaryOpc_match<LHS, RHS, true> m_FAdd(const LHS &L, const RHS &R) {
547 return BinaryOpc_match<LHS, RHS, true>(ISD::FADD, L, R);
548 }
549
550 template <typename LHS, typename RHS>
551 inline BinaryOpc_match<LHS, RHS, false> m_FSub(const LHS &L, const RHS &R) {
552 return BinaryOpc_match<LHS, RHS, false>(ISD::FSUB, L, R);
553 }
554
555 template <typename LHS, typename RHS>
556 inline BinaryOpc_match<LHS, RHS, true> m_FMul(const LHS &L, const RHS &R) {
557 return BinaryOpc_match<LHS, RHS, true>(ISD::FMUL, L, R);
558 }
559
560 template <typename LHS, typename RHS>
561 inline BinaryOpc_match<LHS, RHS, false> m_FDiv(const LHS &L, const RHS &R) {
562 return BinaryOpc_match<LHS, RHS, false>(ISD::FDIV, L, R);
563 }
564
565 template <typename LHS, typename RHS>
566 inline BinaryOpc_match<LHS, RHS, false> m_FRem(const LHS &L, const RHS &R) {
567 return BinaryOpc_match<LHS, RHS, false>(ISD::FREM, L, R);
568 }
569
570 // === Unary operations ===
571 template <typename Opnd_P, bool ExcludeChain = false> struct UnaryOpc_match {
572 unsigned Opcode;
573 Opnd_P Opnd;
574
575 UnaryOpc_match(unsigned Opc, const Opnd_P &Op) : Opcode(Opc), Opnd(Op) {}
576
577 template <typename MatchContext>
578 bool match(const MatchContext &Ctx, SDValue N) {
579 if (sd_context_match(N, Ctx, m_Opc(Opcode))) {
580 EffectiveOperands<ExcludeChain> EO(N);
581 assert(EO.Size == 1);
582 return Opnd.match(Ctx, N->getOperand(EO.FirstIndex));
583 }
584
585 return false;
586 }
587 };
588
589 template <typename Opnd>
590 inline UnaryOpc_match<Opnd> m_UnaryOp(unsigned Opc, const Opnd &Op) {
591 return UnaryOpc_match<Opnd>(Opc, Op);
592 }
593 template <typename Opnd>
594 inline UnaryOpc_match<Opnd, true> m_ChainedUnaryOp(unsigned Opc,
595 const Opnd &Op) {
596 return UnaryOpc_match<Opnd, true>(Opc, Op);
597 }
598
599 template <typename Opnd> inline UnaryOpc_match<Opnd> m_ZExt(const Opnd &Op) {
600 return UnaryOpc_match<Opnd>(ISD::ZERO_EXTEND, Op);
601 }
602
603 template <typename Opnd> inline UnaryOpc_match<Opnd> m_SExt(const Opnd &Op) {
604 return UnaryOpc_match<Opnd>(ISD::SIGN_EXTEND, Op);
605 }
606
607 template <typename Opnd> inline UnaryOpc_match<Opnd> m_AnyExt(const Opnd &Op) {
608 return UnaryOpc_match<Opnd>(ISD::ANY_EXTEND, Op);
609 }
610
611 template <typename Opnd> inline UnaryOpc_match<Opnd> m_Trunc(const Opnd &Op) {
612 return UnaryOpc_match<Opnd>(ISD::TRUNCATE, Op);
613 }
614
615 // === Constants ===
616 struct ConstantInt_match {
617 APInt *BindVal;
618
619 explicit ConstantInt_match(APInt *V) : BindVal(V) {}
620
621 template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
622 // The logics here are similar to that in
623 // SelectionDAG::isConstantIntBuildVectorOrConstantInt, but the latter also
624 // treats GlobalAddressSDNode as a constant, which is difficult to turn into
625 // APInt.
626 if (auto *C = dyn_cast_or_null<ConstantSDNode>(N.getNode())) {
627 if (BindVal)
628 *BindVal = C->getAPIntValue();
629 return true;
630 }
631
632 APInt Discard;
633 return ISD::isConstantSplatVector(N.getNode(),
634 BindVal ? *BindVal : Discard);
635 }
636 };
637 /// Match any interger constants or splat of an integer constant.
638 inline ConstantInt_match m_ConstInt() { return ConstantInt_match(nullptr); }
639 /// Match any interger constants or splat of an integer constant; return the
640 /// specific constant or constant splat value.
641 inline ConstantInt_match m_ConstInt(APInt &V) { return ConstantInt_match(&V); }
642
643 struct SpecificInt_match {
644 APInt IntVal;
645
646 explicit SpecificInt_match(APInt APV) : IntVal(std::move(APV)) {}
647
648 template <typename MatchContext>
649 bool match(const MatchContext &Ctx, SDValue N) {
650 APInt ConstInt;
651 if (sd_context_match(N, Ctx, m_ConstInt(ConstInt)))
652 return APInt::isSameValue(IntVal, ConstInt);
653 return false;
654 }
655 };
656
657 /// Match a specific integer constant or constant splat value.
658 inline SpecificInt_match m_SpecificInt(APInt V) {
659 return SpecificInt_match(std::move(V));
660 }
661 inline SpecificInt_match m_SpecificInt(uint64_t V) {
662 return SpecificInt_match(APInt(64, V));
663 }
664
665 inline SpecificInt_match m_Zero() { return m_SpecificInt(0U); }
666 inline SpecificInt_match m_One() { return m_SpecificInt(1U); }
667 inline SpecificInt_match m_AllOnes() { return m_SpecificInt(~0U); }
668
669 /// Match true boolean value based on the information provided by
670 /// TargetLowering.
671 inline auto m_True() {
672 return TLI_pred_match{
673 [](const TargetLowering &TLI, SDValue N) {
674 APInt ConstVal;
675 if (sd_match(N, m_ConstInt(ConstVal)))
676 switch (TLI.getBooleanContents(N.getValueType())) {
677 case TargetLowering::ZeroOrOneBooleanContent:
678 return ConstVal.isOne();
679 case TargetLowering::ZeroOrNegativeOneBooleanContent:
680 return ConstVal.isAllOnes();
681 case TargetLowering::UndefinedBooleanContent:
682 return (ConstVal & 0x01) == 1;
683 }
684
685 return false;
686 },
687 m_Value()};
688 }
689 /// Match false boolean value based on the information provided by
690 /// TargetLowering.
691 inline auto m_False() {
692 return TLI_pred_match{
693 [](const TargetLowering &TLI, SDValue N) {
694 APInt ConstVal;
695 if (sd_match(N, m_ConstInt(ConstVal)))
696 switch (TLI.getBooleanContents(N.getValueType())) {
697 case TargetLowering::ZeroOrOneBooleanContent:
698 case TargetLowering::ZeroOrNegativeOneBooleanContent:
699 return ConstVal.isZero();
700 case TargetLowering::UndefinedBooleanContent:
701 return (ConstVal & 0x01) == 0;
702 }
703
704 return false;
705 },
706 m_Value()};
707 }
708 } // namespace SDPatternMatch
709 } // namespace llvm
710 #endif
711