blob: 1a11af27d1a3370d71ad21ea49eae17877aa8dcc [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file provides a simple and efficient mechanism for performing general
10// tree-based pattern matches on the LLVM IR. The power of these routines is
11// that it allows you to write concise patterns that are expressive and easy to
12// understand. The other major advantage of this is that it allows you to
13// trivially capture/bind elements in the pattern to variables. For example,
14// you can do something like this:
15//
16// Value *Exp = ...
17// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
18// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
19// m_And(m_Value(Y), m_ConstantInt(C2))))) {
20// ... Pattern is matched and variables are bound ...
21// }
22//
23// This is primarily useful to things like the instruction combiner, but can
24// also be useful for static analysis tools or code generators.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_IR_PATTERNMATCH_H
29#define LLVM_IR_PATTERNMATCH_H
30
31#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033#include "llvm/IR/Constant.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/InstrTypes.h"
36#include "llvm/IR/Instruction.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/Intrinsics.h"
39#include "llvm/IR/Operator.h"
40#include "llvm/IR/Value.h"
41#include "llvm/Support/Casting.h"
42#include <cstdint>
43
44namespace llvm {
45namespace PatternMatch {
46
47template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
48 return const_cast<Pattern &>(P).match(V);
49}
50
51template <typename SubPattern_t> struct OneUse_match {
52 SubPattern_t SubPattern;
53
54 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
55
56 template <typename OpTy> bool match(OpTy *V) {
57 return V->hasOneUse() && SubPattern.match(V);
58 }
59};
60
61template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
62 return SubPattern;
63}
64
65template <typename Class> struct class_match {
66 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
67};
68
69/// Match an arbitrary value and ignore it.
70inline class_match<Value> m_Value() { return class_match<Value>(); }
71
72/// Match an arbitrary binary operation and ignore it.
73inline class_match<BinaryOperator> m_BinOp() {
74 return class_match<BinaryOperator>();
75}
76
77/// Matches any compare instruction and ignore it.
78inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
79
80/// Match an arbitrary ConstantInt and ignore it.
81inline class_match<ConstantInt> m_ConstantInt() {
82 return class_match<ConstantInt>();
83}
84
85/// Match an arbitrary undef constant.
86inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
87
88/// Match an arbitrary Constant and ignore it.
89inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
90
91/// Matching combinators
92template <typename LTy, typename RTy> struct match_combine_or {
93 LTy L;
94 RTy R;
95
96 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
97
98 template <typename ITy> bool match(ITy *V) {
99 if (L.match(V))
100 return true;
101 if (R.match(V))
102 return true;
103 return false;
104 }
105};
106
107template <typename LTy, typename RTy> struct match_combine_and {
108 LTy L;
109 RTy R;
110
111 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
112
113 template <typename ITy> bool match(ITy *V) {
114 if (L.match(V))
115 if (R.match(V))
116 return true;
117 return false;
118 }
119};
120
121/// Combine two pattern matchers matching L || R
122template <typename LTy, typename RTy>
123inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
124 return match_combine_or<LTy, RTy>(L, R);
125}
126
127/// Combine two pattern matchers matching L && R
128template <typename LTy, typename RTy>
129inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
130 return match_combine_and<LTy, RTy>(L, R);
131}
132
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100133struct apint_match {
134 const APInt *&Res;
135
136 apint_match(const APInt *&R) : Res(R) {}
137
138 template <typename ITy> bool match(ITy *V) {
139 if (auto *CI = dyn_cast<ConstantInt>(V)) {
140 Res = &CI->getValue();
141 return true;
142 }
143 if (V->getType()->isVectorTy())
144 if (const auto *C = dyn_cast<Constant>(V))
145 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
146 Res = &CI->getValue();
147 return true;
148 }
149 return false;
150 }
151};
152// Either constexpr if or renaming ConstantFP::getValueAPF to
153// ConstantFP::getValue is needed to do it via single template
154// function for both apint/apfloat.
155struct apfloat_match {
156 const APFloat *&Res;
157 apfloat_match(const APFloat *&R) : Res(R) {}
158 template <typename ITy> bool match(ITy *V) {
159 if (auto *CI = dyn_cast<ConstantFP>(V)) {
160 Res = &CI->getValueAPF();
161 return true;
162 }
163 if (V->getType()->isVectorTy())
164 if (const auto *C = dyn_cast<Constant>(V))
165 if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) {
166 Res = &CI->getValueAPF();
167 return true;
168 }
169 return false;
170 }
171};
172
173/// Match a ConstantInt or splatted ConstantVector, binding the
174/// specified pointer to the contained APInt.
175inline apint_match m_APInt(const APInt *&Res) { return Res; }
176
177/// Match a ConstantFP or splatted ConstantVector, binding the
178/// specified pointer to the contained APFloat.
179inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
180
181template <int64_t Val> struct constantint_match {
182 template <typename ITy> bool match(ITy *V) {
183 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
184 const APInt &CIV = CI->getValue();
185 if (Val >= 0)
186 return CIV == static_cast<uint64_t>(Val);
187 // If Val is negative, and CI is shorter than it, truncate to the right
188 // number of bits. If it is larger, then we have to sign extend. Just
189 // compare their negated values.
190 return -CIV == -Val;
191 }
192 return false;
193 }
194};
195
196/// Match a ConstantInt with a specific value.
197template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
198 return constantint_match<Val>();
199}
200
201/// This helper class is used to match scalar and vector integer constants that
202/// satisfy a specified predicate.
203/// For vector constants, undefined elements are ignored.
204template <typename Predicate> struct cst_pred_ty : public Predicate {
205 template <typename ITy> bool match(ITy *V) {
206 if (const auto *CI = dyn_cast<ConstantInt>(V))
207 return this->isValue(CI->getValue());
208 if (V->getType()->isVectorTy()) {
209 if (const auto *C = dyn_cast<Constant>(V)) {
210 if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
211 return this->isValue(CI->getValue());
212
213 // Non-splat vector constant: check each element for a match.
214 unsigned NumElts = V->getType()->getVectorNumElements();
215 assert(NumElts != 0 && "Constant vector with no elements?");
Andrew Walbran16937d02019-10-22 13:54:20 +0100216 bool HasNonUndefElements = false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217 for (unsigned i = 0; i != NumElts; ++i) {
218 Constant *Elt = C->getAggregateElement(i);
219 if (!Elt)
220 return false;
221 if (isa<UndefValue>(Elt))
222 continue;
223 auto *CI = dyn_cast<ConstantInt>(Elt);
224 if (!CI || !this->isValue(CI->getValue()))
225 return false;
Andrew Walbran16937d02019-10-22 13:54:20 +0100226 HasNonUndefElements = true;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100227 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100228 return HasNonUndefElements;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229 }
230 }
231 return false;
232 }
233};
234
235/// This helper class is used to match scalar and vector constants that
236/// satisfy a specified predicate, and bind them to an APInt.
237template <typename Predicate> struct api_pred_ty : public Predicate {
238 const APInt *&Res;
239
240 api_pred_ty(const APInt *&R) : Res(R) {}
241
242 template <typename ITy> bool match(ITy *V) {
243 if (const auto *CI = dyn_cast<ConstantInt>(V))
244 if (this->isValue(CI->getValue())) {
245 Res = &CI->getValue();
246 return true;
247 }
248 if (V->getType()->isVectorTy())
249 if (const auto *C = dyn_cast<Constant>(V))
250 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
251 if (this->isValue(CI->getValue())) {
252 Res = &CI->getValue();
253 return true;
254 }
255
256 return false;
257 }
258};
259
260/// This helper class is used to match scalar and vector floating-point
261/// constants that satisfy a specified predicate.
262/// For vector constants, undefined elements are ignored.
263template <typename Predicate> struct cstfp_pred_ty : public Predicate {
264 template <typename ITy> bool match(ITy *V) {
265 if (const auto *CF = dyn_cast<ConstantFP>(V))
266 return this->isValue(CF->getValueAPF());
267 if (V->getType()->isVectorTy()) {
268 if (const auto *C = dyn_cast<Constant>(V)) {
269 if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
270 return this->isValue(CF->getValueAPF());
271
272 // Non-splat vector constant: check each element for a match.
273 unsigned NumElts = V->getType()->getVectorNumElements();
274 assert(NumElts != 0 && "Constant vector with no elements?");
Andrew Walbran16937d02019-10-22 13:54:20 +0100275 bool HasNonUndefElements = false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100276 for (unsigned i = 0; i != NumElts; ++i) {
277 Constant *Elt = C->getAggregateElement(i);
278 if (!Elt)
279 return false;
280 if (isa<UndefValue>(Elt))
281 continue;
282 auto *CF = dyn_cast<ConstantFP>(Elt);
283 if (!CF || !this->isValue(CF->getValueAPF()))
284 return false;
Andrew Walbran16937d02019-10-22 13:54:20 +0100285 HasNonUndefElements = true;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100286 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100287 return HasNonUndefElements;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100288 }
289 }
290 return false;
291 }
292};
293
294///////////////////////////////////////////////////////////////////////////////
295//
296// Encapsulate constant value queries for use in templated predicate matchers.
297// This allows checking if constants match using compound predicates and works
298// with vector constants, possibly with relaxed constraints. For example, ignore
299// undef values.
300//
301///////////////////////////////////////////////////////////////////////////////
302
303struct is_all_ones {
304 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
305};
306/// Match an integer or vector with all bits set.
307/// For vectors, this includes constants with undefined elements.
308inline cst_pred_ty<is_all_ones> m_AllOnes() {
309 return cst_pred_ty<is_all_ones>();
310}
311
312struct is_maxsignedvalue {
313 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
314};
315/// Match an integer or vector with values having all bits except for the high
316/// bit set (0x7f...).
317/// For vectors, this includes constants with undefined elements.
318inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
319 return cst_pred_ty<is_maxsignedvalue>();
320}
321inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
322 return V;
323}
324
325struct is_negative {
326 bool isValue(const APInt &C) { return C.isNegative(); }
327};
328/// Match an integer or vector of negative values.
329/// For vectors, this includes constants with undefined elements.
330inline cst_pred_ty<is_negative> m_Negative() {
331 return cst_pred_ty<is_negative>();
332}
333inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
334 return V;
335}
336
337struct is_nonnegative {
338 bool isValue(const APInt &C) { return C.isNonNegative(); }
339};
340/// Match an integer or vector of nonnegative values.
341/// For vectors, this includes constants with undefined elements.
342inline cst_pred_ty<is_nonnegative> m_NonNegative() {
343 return cst_pred_ty<is_nonnegative>();
344}
345inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
346 return V;
347}
348
349struct is_one {
350 bool isValue(const APInt &C) { return C.isOneValue(); }
351};
352/// Match an integer 1 or a vector with all elements equal to 1.
353/// For vectors, this includes constants with undefined elements.
354inline cst_pred_ty<is_one> m_One() {
355 return cst_pred_ty<is_one>();
356}
357
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100358struct is_zero_int {
359 bool isValue(const APInt &C) { return C.isNullValue(); }
360};
361/// Match an integer 0 or a vector with all elements equal to 0.
362/// For vectors, this includes constants with undefined elements.
363inline cst_pred_ty<is_zero_int> m_ZeroInt() {
364 return cst_pred_ty<is_zero_int>();
365}
366
367struct is_zero {
368 template <typename ITy> bool match(ITy *V) {
369 auto *C = dyn_cast<Constant>(V);
370 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
371 }
372};
373/// Match any null constant or a vector with all elements equal to 0.
374/// For vectors, this includes constants with undefined elements.
375inline is_zero m_Zero() {
376 return is_zero();
377}
378
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100379struct is_power2 {
380 bool isValue(const APInt &C) { return C.isPowerOf2(); }
381};
382/// Match an integer or vector power-of-2.
383/// For vectors, this includes constants with undefined elements.
384inline cst_pred_ty<is_power2> m_Power2() {
385 return cst_pred_ty<is_power2>();
386}
387inline api_pred_ty<is_power2> m_Power2(const APInt *&V) {
388 return V;
389}
390
391struct is_power2_or_zero {
392 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
393};
394/// Match an integer or vector of 0 or power-of-2 values.
395/// For vectors, this includes constants with undefined elements.
396inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
397 return cst_pred_ty<is_power2_or_zero>();
398}
399inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
400 return V;
401}
402
403struct is_sign_mask {
404 bool isValue(const APInt &C) { return C.isSignMask(); }
405};
406/// Match an integer or vector with only the sign bit(s) set.
407/// For vectors, this includes constants with undefined elements.
408inline cst_pred_ty<is_sign_mask> m_SignMask() {
409 return cst_pred_ty<is_sign_mask>();
410}
411
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100412struct is_lowbit_mask {
413 bool isValue(const APInt &C) { return C.isMask(); }
414};
415/// Match an integer or vector with only the low bit(s) set.
416/// For vectors, this includes constants with undefined elements.
417inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
418 return cst_pred_ty<is_lowbit_mask>();
419}
420
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100421struct is_unsigned_less_than {
422 const APInt *Thr;
423 bool isValue(const APInt &C) { return C.ult(*Thr); }
424};
425/// Match an integer or vector with every element unsigned less than the
426/// Threshold. For vectors, this includes constants with undefined elements.
427/// FIXME: is it worth generalizing this to simply take ICmpInst::Predicate?
428inline cst_pred_ty<is_unsigned_less_than>
429m_SpecificInt_ULT(const APInt &Threshold) {
430 cst_pred_ty<is_unsigned_less_than> P;
431 P.Thr = &Threshold;
432 return P;
433}
434
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100435struct is_nan {
436 bool isValue(const APFloat &C) { return C.isNaN(); }
437};
438/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
439/// For vectors, this includes constants with undefined elements.
440inline cstfp_pred_ty<is_nan> m_NaN() {
441 return cstfp_pred_ty<is_nan>();
442}
443
444struct is_any_zero_fp {
445 bool isValue(const APFloat &C) { return C.isZero(); }
446};
447/// Match a floating-point negative zero or positive zero.
448/// For vectors, this includes constants with undefined elements.
449inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
450 return cstfp_pred_ty<is_any_zero_fp>();
451}
452
453struct is_pos_zero_fp {
454 bool isValue(const APFloat &C) { return C.isPosZero(); }
455};
456/// Match a floating-point positive zero.
457/// For vectors, this includes constants with undefined elements.
458inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
459 return cstfp_pred_ty<is_pos_zero_fp>();
460}
461
462struct is_neg_zero_fp {
463 bool isValue(const APFloat &C) { return C.isNegZero(); }
464};
465/// Match a floating-point negative zero.
466/// For vectors, this includes constants with undefined elements.
467inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
468 return cstfp_pred_ty<is_neg_zero_fp>();
469}
470
471///////////////////////////////////////////////////////////////////////////////
472
473template <typename Class> struct bind_ty {
474 Class *&VR;
475
476 bind_ty(Class *&V) : VR(V) {}
477
478 template <typename ITy> bool match(ITy *V) {
479 if (auto *CV = dyn_cast<Class>(V)) {
480 VR = CV;
481 return true;
482 }
483 return false;
484 }
485};
486
487/// Match a value, capturing it if we match.
488inline bind_ty<Value> m_Value(Value *&V) { return V; }
489inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
490
491/// Match an instruction, capturing it if we match.
492inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
493/// Match a binary operator, capturing it if we match.
494inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
495
496/// Match a ConstantInt, capturing the value if we match.
497inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
498
499/// Match a Constant, capturing the value if we match.
500inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
501
502/// Match a ConstantFP, capturing the value if we match.
503inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
504
505/// Match a specified Value*.
506struct specificval_ty {
507 const Value *Val;
508
509 specificval_ty(const Value *V) : Val(V) {}
510
511 template <typename ITy> bool match(ITy *V) { return V == Val; }
512};
513
514/// Match if we have a specific specified value.
515inline specificval_ty m_Specific(const Value *V) { return V; }
516
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100517/// Stores a reference to the Value *, not the Value * itself,
518/// thus can be used in commutative matchers.
519template <typename Class> struct deferredval_ty {
520 Class *const &Val;
521
522 deferredval_ty(Class *const &V) : Val(V) {}
523
524 template <typename ITy> bool match(ITy *const V) { return V == Val; }
525};
526
527/// A commutative-friendly version of m_Specific().
528inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
529inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
530 return V;
531}
532
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100533/// Match a specified floating point value or vector of all elements of
534/// that value.
535struct specific_fpval {
536 double Val;
537
538 specific_fpval(double V) : Val(V) {}
539
540 template <typename ITy> bool match(ITy *V) {
541 if (const auto *CFP = dyn_cast<ConstantFP>(V))
542 return CFP->isExactlyValue(Val);
543 if (V->getType()->isVectorTy())
544 if (const auto *C = dyn_cast<Constant>(V))
545 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
546 return CFP->isExactlyValue(Val);
547 return false;
548 }
549};
550
551/// Match a specific floating point value or vector with all elements
552/// equal to the value.
553inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
554
555/// Match a float 1.0 or vector with all elements equal to 1.0.
556inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
557
558struct bind_const_intval_ty {
559 uint64_t &VR;
560
561 bind_const_intval_ty(uint64_t &V) : VR(V) {}
562
563 template <typename ITy> bool match(ITy *V) {
564 if (const auto *CV = dyn_cast<ConstantInt>(V))
565 if (CV->getValue().ule(UINT64_MAX)) {
566 VR = CV->getZExtValue();
567 return true;
568 }
569 return false;
570 }
571};
572
573/// Match a specified integer value or vector of all elements of that
574// value.
575struct specific_intval {
576 uint64_t Val;
577
578 specific_intval(uint64_t V) : Val(V) {}
579
580 template <typename ITy> bool match(ITy *V) {
581 const auto *CI = dyn_cast<ConstantInt>(V);
582 if (!CI && V->getType()->isVectorTy())
583 if (const auto *C = dyn_cast<Constant>(V))
584 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
585
586 return CI && CI->getValue() == Val;
587 }
588};
589
590/// Match a specific integer value or vector with all elements equal to
591/// the value.
592inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
593
594/// Match a ConstantInt and bind to its value. This does not match
595/// ConstantInts wider than 64-bits.
596inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
597
598//===----------------------------------------------------------------------===//
599// Matcher for any binary operator.
600//
601template <typename LHS_t, typename RHS_t, bool Commutable = false>
602struct AnyBinaryOp_match {
603 LHS_t L;
604 RHS_t R;
605
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100606 // The evaluation order is always stable, regardless of Commutability.
607 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100608 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
609
610 template <typename OpTy> bool match(OpTy *V) {
611 if (auto *I = dyn_cast<BinaryOperator>(V))
612 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100613 (Commutable && L.match(I->getOperand(1)) &&
614 R.match(I->getOperand(0)));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100615 return false;
616 }
617};
618
619template <typename LHS, typename RHS>
620inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
621 return AnyBinaryOp_match<LHS, RHS>(L, R);
622}
623
624//===----------------------------------------------------------------------===//
625// Matchers for specific binary operators.
626//
627
628template <typename LHS_t, typename RHS_t, unsigned Opcode,
629 bool Commutable = false>
630struct BinaryOp_match {
631 LHS_t L;
632 RHS_t R;
633
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100634 // The evaluation order is always stable, regardless of Commutability.
635 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100636 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
637
638 template <typename OpTy> bool match(OpTy *V) {
639 if (V->getValueID() == Value::InstructionVal + Opcode) {
640 auto *I = cast<BinaryOperator>(V);
641 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100642 (Commutable && L.match(I->getOperand(1)) &&
643 R.match(I->getOperand(0)));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100644 }
645 if (auto *CE = dyn_cast<ConstantExpr>(V))
646 return CE->getOpcode() == Opcode &&
647 ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100648 (Commutable && L.match(CE->getOperand(1)) &&
649 R.match(CE->getOperand(0))));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100650 return false;
651 }
652};
653
654template <typename LHS, typename RHS>
655inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
656 const RHS &R) {
657 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
658}
659
660template <typename LHS, typename RHS>
661inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
662 const RHS &R) {
663 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
664}
665
666template <typename LHS, typename RHS>
667inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
668 const RHS &R) {
669 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
670}
671
672template <typename LHS, typename RHS>
673inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
674 const RHS &R) {
675 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
676}
677
Andrew Walbran16937d02019-10-22 13:54:20 +0100678template <typename Op_t> struct FNeg_match {
679 Op_t X;
680
681 FNeg_match(const Op_t &Op) : X(Op) {}
682 template <typename OpTy> bool match(OpTy *V) {
683 auto *FPMO = dyn_cast<FPMathOperator>(V);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100684 if (!FPMO) return false;
685
686 if (FPMO->getOpcode() == Instruction::FNeg)
687 return X.match(FPMO->getOperand(0));
688
689 if (FPMO->getOpcode() == Instruction::FSub) {
690 if (FPMO->hasNoSignedZeros()) {
691 // With 'nsz', any zero goes.
692 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
693 return false;
694 } else {
695 // Without 'nsz', we need fsub -0.0, X exactly.
696 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
697 return false;
698 }
699
700 return X.match(FPMO->getOperand(1));
Andrew Walbran16937d02019-10-22 13:54:20 +0100701 }
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100702
703 return false;
Andrew Walbran16937d02019-10-22 13:54:20 +0100704 }
705};
706
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100707/// Match 'fneg X' as 'fsub -0.0, X'.
Andrew Walbran16937d02019-10-22 13:54:20 +0100708template <typename OpTy>
709inline FNeg_match<OpTy>
710m_FNeg(const OpTy &X) {
711 return FNeg_match<OpTy>(X);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100712}
713
Andrew Scull0372a572018-11-16 15:47:06 +0000714/// Match 'fneg X' as 'fsub +-0.0, X'.
715template <typename RHS>
716inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
717m_FNegNSZ(const RHS &X) {
718 return m_FSub(m_AnyZeroFP(), X);
719}
720
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100721template <typename LHS, typename RHS>
722inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
723 const RHS &R) {
724 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
725}
726
727template <typename LHS, typename RHS>
728inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
729 const RHS &R) {
730 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
731}
732
733template <typename LHS, typename RHS>
734inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
735 const RHS &R) {
736 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
737}
738
739template <typename LHS, typename RHS>
740inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
741 const RHS &R) {
742 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
743}
744
745template <typename LHS, typename RHS>
746inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
747 const RHS &R) {
748 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
749}
750
751template <typename LHS, typename RHS>
752inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
753 const RHS &R) {
754 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
755}
756
757template <typename LHS, typename RHS>
758inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
759 const RHS &R) {
760 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
761}
762
763template <typename LHS, typename RHS>
764inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
765 const RHS &R) {
766 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
767}
768
769template <typename LHS, typename RHS>
770inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
771 const RHS &R) {
772 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
773}
774
775template <typename LHS, typename RHS>
776inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
777 const RHS &R) {
778 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
779}
780
781template <typename LHS, typename RHS>
782inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
783 const RHS &R) {
784 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
785}
786
787template <typename LHS, typename RHS>
788inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
789 const RHS &R) {
790 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
791}
792
793template <typename LHS, typename RHS>
794inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
795 const RHS &R) {
796 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
797}
798
799template <typename LHS, typename RHS>
800inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
801 const RHS &R) {
802 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
803}
804
805template <typename LHS_t, typename RHS_t, unsigned Opcode,
806 unsigned WrapFlags = 0>
807struct OverflowingBinaryOp_match {
808 LHS_t L;
809 RHS_t R;
810
811 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
812 : L(LHS), R(RHS) {}
813
814 template <typename OpTy> bool match(OpTy *V) {
815 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
816 if (Op->getOpcode() != Opcode)
817 return false;
818 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
819 !Op->hasNoUnsignedWrap())
820 return false;
821 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
822 !Op->hasNoSignedWrap())
823 return false;
824 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
825 }
826 return false;
827 }
828};
829
830template <typename LHS, typename RHS>
831inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
832 OverflowingBinaryOperator::NoSignedWrap>
833m_NSWAdd(const LHS &L, const RHS &R) {
834 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
835 OverflowingBinaryOperator::NoSignedWrap>(
836 L, R);
837}
838template <typename LHS, typename RHS>
839inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
840 OverflowingBinaryOperator::NoSignedWrap>
841m_NSWSub(const LHS &L, const RHS &R) {
842 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
843 OverflowingBinaryOperator::NoSignedWrap>(
844 L, R);
845}
846template <typename LHS, typename RHS>
847inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
848 OverflowingBinaryOperator::NoSignedWrap>
849m_NSWMul(const LHS &L, const RHS &R) {
850 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
851 OverflowingBinaryOperator::NoSignedWrap>(
852 L, R);
853}
854template <typename LHS, typename RHS>
855inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
856 OverflowingBinaryOperator::NoSignedWrap>
857m_NSWShl(const LHS &L, const RHS &R) {
858 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
859 OverflowingBinaryOperator::NoSignedWrap>(
860 L, R);
861}
862
863template <typename LHS, typename RHS>
864inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
865 OverflowingBinaryOperator::NoUnsignedWrap>
866m_NUWAdd(const LHS &L, const RHS &R) {
867 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
868 OverflowingBinaryOperator::NoUnsignedWrap>(
869 L, R);
870}
871template <typename LHS, typename RHS>
872inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
873 OverflowingBinaryOperator::NoUnsignedWrap>
874m_NUWSub(const LHS &L, const RHS &R) {
875 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
876 OverflowingBinaryOperator::NoUnsignedWrap>(
877 L, R);
878}
879template <typename LHS, typename RHS>
880inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
881 OverflowingBinaryOperator::NoUnsignedWrap>
882m_NUWMul(const LHS &L, const RHS &R) {
883 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
884 OverflowingBinaryOperator::NoUnsignedWrap>(
885 L, R);
886}
887template <typename LHS, typename RHS>
888inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
889 OverflowingBinaryOperator::NoUnsignedWrap>
890m_NUWShl(const LHS &L, const RHS &R) {
891 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
892 OverflowingBinaryOperator::NoUnsignedWrap>(
893 L, R);
894}
895
896//===----------------------------------------------------------------------===//
897// Class that matches a group of binary opcodes.
898//
899template <typename LHS_t, typename RHS_t, typename Predicate>
900struct BinOpPred_match : Predicate {
901 LHS_t L;
902 RHS_t R;
903
904 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
905
906 template <typename OpTy> bool match(OpTy *V) {
907 if (auto *I = dyn_cast<Instruction>(V))
908 return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
909 R.match(I->getOperand(1));
910 if (auto *CE = dyn_cast<ConstantExpr>(V))
911 return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
912 R.match(CE->getOperand(1));
913 return false;
914 }
915};
916
917struct is_shift_op {
918 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
919};
920
921struct is_right_shift_op {
922 bool isOpType(unsigned Opcode) {
923 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
924 }
925};
926
927struct is_logical_shift_op {
928 bool isOpType(unsigned Opcode) {
929 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
930 }
931};
932
933struct is_bitwiselogic_op {
934 bool isOpType(unsigned Opcode) {
935 return Instruction::isBitwiseLogicOp(Opcode);
936 }
937};
938
939struct is_idiv_op {
940 bool isOpType(unsigned Opcode) {
941 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
942 }
943};
944
945/// Matches shift operations.
946template <typename LHS, typename RHS>
947inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
948 const RHS &R) {
949 return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
950}
951
952/// Matches logical shift operations.
953template <typename LHS, typename RHS>
954inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
955 const RHS &R) {
956 return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
957}
958
959/// Matches logical shift operations.
960template <typename LHS, typename RHS>
961inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
962m_LogicalShift(const LHS &L, const RHS &R) {
963 return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
964}
965
966/// Matches bitwise logic operations.
967template <typename LHS, typename RHS>
968inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
969m_BitwiseLogic(const LHS &L, const RHS &R) {
970 return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
971}
972
973/// Matches integer division operations.
974template <typename LHS, typename RHS>
975inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
976 const RHS &R) {
977 return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
978}
979
980//===----------------------------------------------------------------------===//
981// Class that matches exact binary ops.
982//
983template <typename SubPattern_t> struct Exact_match {
984 SubPattern_t SubPattern;
985
986 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
987
988 template <typename OpTy> bool match(OpTy *V) {
989 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
990 return PEO->isExact() && SubPattern.match(V);
991 return false;
992 }
993};
994
995template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
996 return SubPattern;
997}
998
999//===----------------------------------------------------------------------===//
1000// Matchers for CmpInst classes
1001//
1002
1003template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
1004 bool Commutable = false>
1005struct CmpClass_match {
1006 PredicateTy &Predicate;
1007 LHS_t L;
1008 RHS_t R;
1009
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001010 // The evaluation order is always stable, regardless of Commutability.
1011 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001012 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1013 : Predicate(Pred), L(LHS), R(RHS) {}
1014
1015 template <typename OpTy> bool match(OpTy *V) {
1016 if (auto *I = dyn_cast<Class>(V))
1017 if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001018 (Commutable && L.match(I->getOperand(1)) &&
1019 R.match(I->getOperand(0)))) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001020 Predicate = I->getPredicate();
1021 return true;
1022 }
1023 return false;
1024 }
1025};
1026
1027template <typename LHS, typename RHS>
1028inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
1029m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1030 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
1031}
1032
1033template <typename LHS, typename RHS>
1034inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
1035m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1036 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
1037}
1038
1039template <typename LHS, typename RHS>
1040inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
1041m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1042 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
1043}
1044
1045//===----------------------------------------------------------------------===//
Andrew Scull0372a572018-11-16 15:47:06 +00001046// Matchers for instructions with a given opcode and number of operands.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001047//
1048
Andrew Scull0372a572018-11-16 15:47:06 +00001049/// Matches instructions with Opcode and three operands.
1050template <typename T0, unsigned Opcode> struct OneOps_match {
1051 T0 Op1;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001052
Andrew Scull0372a572018-11-16 15:47:06 +00001053 OneOps_match(const T0 &Op1) : Op1(Op1) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001054
1055 template <typename OpTy> bool match(OpTy *V) {
Andrew Scull0372a572018-11-16 15:47:06 +00001056 if (V->getValueID() == Value::InstructionVal + Opcode) {
1057 auto *I = cast<Instruction>(V);
1058 return Op1.match(I->getOperand(0));
1059 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001060 return false;
1061 }
1062};
1063
Andrew Scull0372a572018-11-16 15:47:06 +00001064/// Matches instructions with Opcode and three operands.
1065template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1066 T0 Op1;
1067 T1 Op2;
1068
1069 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1070
1071 template <typename OpTy> bool match(OpTy *V) {
1072 if (V->getValueID() == Value::InstructionVal + Opcode) {
1073 auto *I = cast<Instruction>(V);
1074 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1075 }
1076 return false;
1077 }
1078};
1079
1080/// Matches instructions with Opcode and three operands.
1081template <typename T0, typename T1, typename T2, unsigned Opcode>
1082struct ThreeOps_match {
1083 T0 Op1;
1084 T1 Op2;
1085 T2 Op3;
1086
1087 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1088 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1089
1090 template <typename OpTy> bool match(OpTy *V) {
1091 if (V->getValueID() == Value::InstructionVal + Opcode) {
1092 auto *I = cast<Instruction>(V);
1093 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1094 Op3.match(I->getOperand(2));
1095 }
1096 return false;
1097 }
1098};
1099
1100/// Matches SelectInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001101template <typename Cond, typename LHS, typename RHS>
Andrew Scull0372a572018-11-16 15:47:06 +00001102inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
1103m_Select(const Cond &C, const LHS &L, const RHS &R) {
1104 return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001105}
1106
1107/// This matches a select of two constants, e.g.:
1108/// m_SelectCst<-1, 0>(m_Value(V))
1109template <int64_t L, int64_t R, typename Cond>
Andrew Scull0372a572018-11-16 15:47:06 +00001110inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
1111 Instruction::Select>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001112m_SelectCst(const Cond &C) {
1113 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1114}
1115
Andrew Scull0372a572018-11-16 15:47:06 +00001116/// Matches InsertElementInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001117template <typename Val_t, typename Elt_t, typename Idx_t>
Andrew Scull0372a572018-11-16 15:47:06 +00001118inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001119m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
Andrew Scull0372a572018-11-16 15:47:06 +00001120 return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
1121 Val, Elt, Idx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001122}
1123
Andrew Scull0372a572018-11-16 15:47:06 +00001124/// Matches ExtractElementInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001125template <typename Val_t, typename Idx_t>
Andrew Scull0372a572018-11-16 15:47:06 +00001126inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001127m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
Andrew Scull0372a572018-11-16 15:47:06 +00001128 return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001129}
1130
Andrew Scull0372a572018-11-16 15:47:06 +00001131/// Matches ShuffleVectorInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001132template <typename V1_t, typename V2_t, typename Mask_t>
Andrew Scull0372a572018-11-16 15:47:06 +00001133inline ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001134m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
Andrew Scull0372a572018-11-16 15:47:06 +00001135 return ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>(v1, v2,
1136 m);
1137}
1138
1139/// Matches LoadInst.
1140template <typename OpTy>
1141inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
1142 return OneOps_match<OpTy, Instruction::Load>(Op);
1143}
1144
1145/// Matches StoreInst.
1146template <typename ValueOpTy, typename PointerOpTy>
1147inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
1148m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1149 return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
1150 PointerOp);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001151}
1152
1153//===----------------------------------------------------------------------===//
1154// Matchers for CastInst classes
1155//
1156
1157template <typename Op_t, unsigned Opcode> struct CastClass_match {
1158 Op_t Op;
1159
1160 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1161
1162 template <typename OpTy> bool match(OpTy *V) {
1163 if (auto *O = dyn_cast<Operator>(V))
1164 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1165 return false;
1166 }
1167};
1168
1169/// Matches BitCast.
1170template <typename OpTy>
1171inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
1172 return CastClass_match<OpTy, Instruction::BitCast>(Op);
1173}
1174
1175/// Matches PtrToInt.
1176template <typename OpTy>
1177inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
1178 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
1179}
1180
1181/// Matches Trunc.
1182template <typename OpTy>
1183inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1184 return CastClass_match<OpTy, Instruction::Trunc>(Op);
1185}
1186
1187/// Matches SExt.
1188template <typename OpTy>
1189inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
1190 return CastClass_match<OpTy, Instruction::SExt>(Op);
1191}
1192
1193/// Matches ZExt.
1194template <typename OpTy>
1195inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
1196 return CastClass_match<OpTy, Instruction::ZExt>(Op);
1197}
1198
1199template <typename OpTy>
1200inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1201 CastClass_match<OpTy, Instruction::SExt>>
1202m_ZExtOrSExt(const OpTy &Op) {
1203 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1204}
1205
1206/// Matches UIToFP.
1207template <typename OpTy>
1208inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
1209 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
1210}
1211
1212/// Matches SIToFP.
1213template <typename OpTy>
1214inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
1215 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
1216}
1217
1218/// Matches FPTrunc
1219template <typename OpTy>
1220inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
1221 return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
1222}
1223
1224/// Matches FPExt
1225template <typename OpTy>
1226inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
1227 return CastClass_match<OpTy, Instruction::FPExt>(Op);
1228}
1229
1230//===----------------------------------------------------------------------===//
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001231// Matchers for control flow.
1232//
1233
1234struct br_match {
1235 BasicBlock *&Succ;
1236
1237 br_match(BasicBlock *&Succ) : Succ(Succ) {}
1238
1239 template <typename OpTy> bool match(OpTy *V) {
1240 if (auto *BI = dyn_cast<BranchInst>(V))
1241 if (BI->isUnconditional()) {
1242 Succ = BI->getSuccessor(0);
1243 return true;
1244 }
1245 return false;
1246 }
1247};
1248
1249inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1250
1251template <typename Cond_t> struct brc_match {
1252 Cond_t Cond;
1253 BasicBlock *&T, *&F;
1254
1255 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
1256 : Cond(C), T(t), F(f) {}
1257
1258 template <typename OpTy> bool match(OpTy *V) {
1259 if (auto *BI = dyn_cast<BranchInst>(V))
1260 if (BI->isConditional() && Cond.match(BI->getCondition())) {
1261 T = BI->getSuccessor(0);
1262 F = BI->getSuccessor(1);
1263 return true;
1264 }
1265 return false;
1266 }
1267};
1268
1269template <typename Cond_t>
1270inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1271 return brc_match<Cond_t>(C, T, F);
1272}
1273
1274//===----------------------------------------------------------------------===//
1275// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1276//
1277
1278template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1279 bool Commutable = false>
1280struct MaxMin_match {
1281 LHS_t L;
1282 RHS_t R;
1283
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001284 // The evaluation order is always stable, regardless of Commutability.
1285 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001286 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1287
1288 template <typename OpTy> bool match(OpTy *V) {
1289 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1290 auto *SI = dyn_cast<SelectInst>(V);
1291 if (!SI)
1292 return false;
1293 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1294 if (!Cmp)
1295 return false;
1296 // At this point we have a select conditioned on a comparison. Check that
1297 // it is the values returned by the select that are being compared.
1298 Value *TrueVal = SI->getTrueValue();
1299 Value *FalseVal = SI->getFalseValue();
1300 Value *LHS = Cmp->getOperand(0);
1301 Value *RHS = Cmp->getOperand(1);
1302 if ((TrueVal != LHS || FalseVal != RHS) &&
1303 (TrueVal != RHS || FalseVal != LHS))
1304 return false;
1305 typename CmpInst_t::Predicate Pred =
1306 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1307 // Does "(x pred y) ? x : y" represent the desired max/min operation?
1308 if (!Pred_t::match(Pred))
1309 return false;
1310 // It does! Bind the operands.
1311 return (L.match(LHS) && R.match(RHS)) ||
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001312 (Commutable && L.match(RHS) && R.match(LHS));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001313 }
1314};
1315
1316/// Helper class for identifying signed max predicates.
1317struct smax_pred_ty {
1318 static bool match(ICmpInst::Predicate Pred) {
1319 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1320 }
1321};
1322
1323/// Helper class for identifying signed min predicates.
1324struct smin_pred_ty {
1325 static bool match(ICmpInst::Predicate Pred) {
1326 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1327 }
1328};
1329
1330/// Helper class for identifying unsigned max predicates.
1331struct umax_pred_ty {
1332 static bool match(ICmpInst::Predicate Pred) {
1333 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1334 }
1335};
1336
1337/// Helper class for identifying unsigned min predicates.
1338struct umin_pred_ty {
1339 static bool match(ICmpInst::Predicate Pred) {
1340 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1341 }
1342};
1343
1344/// Helper class for identifying ordered max predicates.
1345struct ofmax_pred_ty {
1346 static bool match(FCmpInst::Predicate Pred) {
1347 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1348 }
1349};
1350
1351/// Helper class for identifying ordered min predicates.
1352struct ofmin_pred_ty {
1353 static bool match(FCmpInst::Predicate Pred) {
1354 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1355 }
1356};
1357
1358/// Helper class for identifying unordered max predicates.
1359struct ufmax_pred_ty {
1360 static bool match(FCmpInst::Predicate Pred) {
1361 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1362 }
1363};
1364
1365/// Helper class for identifying unordered min predicates.
1366struct ufmin_pred_ty {
1367 static bool match(FCmpInst::Predicate Pred) {
1368 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1369 }
1370};
1371
1372template <typename LHS, typename RHS>
1373inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1374 const RHS &R) {
1375 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1376}
1377
1378template <typename LHS, typename RHS>
1379inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1380 const RHS &R) {
1381 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1382}
1383
1384template <typename LHS, typename RHS>
1385inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1386 const RHS &R) {
1387 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1388}
1389
1390template <typename LHS, typename RHS>
1391inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1392 const RHS &R) {
1393 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1394}
1395
1396/// Match an 'ordered' floating point maximum function.
1397/// Floating point has one special value 'NaN'. Therefore, there is no total
1398/// order. However, if we can ignore the 'NaN' value (for example, because of a
1399/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1400/// semantics. In the presence of 'NaN' we have to preserve the original
1401/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1402///
1403/// max(L, R) iff L and R are not NaN
1404/// m_OrdFMax(L, R) = R iff L or R are NaN
1405template <typename LHS, typename RHS>
1406inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1407 const RHS &R) {
1408 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1409}
1410
1411/// Match an 'ordered' floating point minimum function.
1412/// Floating point has one special value 'NaN'. Therefore, there is no total
1413/// order. However, if we can ignore the 'NaN' value (for example, because of a
1414/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1415/// semantics. In the presence of 'NaN' we have to preserve the original
1416/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1417///
1418/// min(L, R) iff L and R are not NaN
1419/// m_OrdFMin(L, R) = R iff L or R are NaN
1420template <typename LHS, typename RHS>
1421inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1422 const RHS &R) {
1423 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1424}
1425
1426/// Match an 'unordered' floating point maximum function.
1427/// Floating point has one special value 'NaN'. Therefore, there is no total
1428/// order. However, if we can ignore the 'NaN' value (for example, because of a
1429/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1430/// semantics. In the presence of 'NaN' we have to preserve the original
1431/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1432///
1433/// max(L, R) iff L and R are not NaN
1434/// m_UnordFMax(L, R) = L iff L or R are NaN
1435template <typename LHS, typename RHS>
1436inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1437m_UnordFMax(const LHS &L, const RHS &R) {
1438 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1439}
1440
1441/// Match an 'unordered' floating point minimum function.
1442/// Floating point has one special value 'NaN'. Therefore, there is no total
1443/// order. However, if we can ignore the 'NaN' value (for example, because of a
1444/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1445/// semantics. In the presence of 'NaN' we have to preserve the original
1446/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1447///
1448/// min(L, R) iff L and R are not NaN
1449/// m_UnordFMin(L, R) = L iff L or R are NaN
1450template <typename LHS, typename RHS>
1451inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1452m_UnordFMin(const LHS &L, const RHS &R) {
1453 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1454}
1455
1456//===----------------------------------------------------------------------===//
1457// Matchers for overflow check patterns: e.g. (a + b) u< a
1458//
1459
1460template <typename LHS_t, typename RHS_t, typename Sum_t>
1461struct UAddWithOverflow_match {
1462 LHS_t L;
1463 RHS_t R;
1464 Sum_t S;
1465
1466 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1467 : L(L), R(R), S(S) {}
1468
1469 template <typename OpTy> bool match(OpTy *V) {
1470 Value *ICmpLHS, *ICmpRHS;
1471 ICmpInst::Predicate Pred;
1472 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1473 return false;
1474
1475 Value *AddLHS, *AddRHS;
1476 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1477
1478 // (a + b) u< a, (a + b) u< b
1479 if (Pred == ICmpInst::ICMP_ULT)
1480 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1481 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1482
1483 // a >u (a + b), b >u (a + b)
1484 if (Pred == ICmpInst::ICMP_UGT)
1485 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1486 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1487
Andrew Walbran16937d02019-10-22 13:54:20 +01001488 // Match special-case for increment-by-1.
1489 if (Pred == ICmpInst::ICMP_EQ) {
1490 // (a + 1) == 0
1491 // (1 + a) == 0
1492 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
1493 (m_One().match(AddLHS) || m_One().match(AddRHS)))
1494 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1495 // 0 == (a + 1)
1496 // 0 == (1 + a)
1497 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
1498 (m_One().match(AddLHS) || m_One().match(AddRHS)))
1499 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1500 }
1501
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001502 return false;
1503 }
1504};
1505
1506/// Match an icmp instruction checking for unsigned overflow on addition.
1507///
1508/// S is matched to the addition whose result is being checked for overflow, and
1509/// L and R are matched to the LHS and RHS of S.
1510template <typename LHS_t, typename RHS_t, typename Sum_t>
1511UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
1512m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1513 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
1514}
1515
1516template <typename Opnd_t> struct Argument_match {
1517 unsigned OpI;
1518 Opnd_t Val;
1519
1520 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1521
1522 template <typename OpTy> bool match(OpTy *V) {
Andrew Walbran16937d02019-10-22 13:54:20 +01001523 // FIXME: Should likely be switched to use `CallBase`.
1524 if (const auto *CI = dyn_cast<CallInst>(V))
1525 return Val.match(CI->getArgOperand(OpI));
1526 return false;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001527 }
1528};
1529
1530/// Match an argument.
1531template <unsigned OpI, typename Opnd_t>
1532inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1533 return Argument_match<Opnd_t>(OpI, Op);
1534}
1535
1536/// Intrinsic matchers.
1537struct IntrinsicID_match {
1538 unsigned ID;
1539
1540 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
1541
1542 template <typename OpTy> bool match(OpTy *V) {
1543 if (const auto *CI = dyn_cast<CallInst>(V))
1544 if (const auto *F = CI->getCalledFunction())
1545 return F->getIntrinsicID() == ID;
1546 return false;
1547 }
1548};
1549
1550/// Intrinsic matches are combinations of ID matchers, and argument
1551/// matchers. Higher arity matcher are defined recursively in terms of and-ing
1552/// them with lower arity matchers. Here's some convenient typedefs for up to
1553/// several arguments, and more can be added as needed
1554template <typename T0 = void, typename T1 = void, typename T2 = void,
1555 typename T3 = void, typename T4 = void, typename T5 = void,
1556 typename T6 = void, typename T7 = void, typename T8 = void,
1557 typename T9 = void, typename T10 = void>
1558struct m_Intrinsic_Ty;
1559template <typename T0> struct m_Intrinsic_Ty<T0> {
1560 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
1561};
1562template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
1563 using Ty =
1564 match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
1565};
1566template <typename T0, typename T1, typename T2>
1567struct m_Intrinsic_Ty<T0, T1, T2> {
1568 using Ty =
1569 match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
1570 Argument_match<T2>>;
1571};
1572template <typename T0, typename T1, typename T2, typename T3>
1573struct m_Intrinsic_Ty<T0, T1, T2, T3> {
1574 using Ty =
1575 match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
1576 Argument_match<T3>>;
1577};
1578
1579/// Match intrinsic calls like this:
1580/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
1581template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1582 return IntrinsicID_match(IntrID);
1583}
1584
1585template <Intrinsic::ID IntrID, typename T0>
1586inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1587 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1588}
1589
1590template <Intrinsic::ID IntrID, typename T0, typename T1>
1591inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
1592 const T1 &Op1) {
1593 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
1594}
1595
1596template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1597inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1598m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1599 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1600}
1601
1602template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1603 typename T3>
1604inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1605m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1606 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1607}
1608
1609// Helper intrinsic matching specializations.
1610template <typename Opnd0>
1611inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
1612 return m_Intrinsic<Intrinsic::bitreverse>(Op0);
1613}
1614
1615template <typename Opnd0>
1616inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1617 return m_Intrinsic<Intrinsic::bswap>(Op0);
1618}
1619
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001620template <typename Opnd0>
1621inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
1622 return m_Intrinsic<Intrinsic::fabs>(Op0);
1623}
1624
1625template <typename Opnd0>
1626inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
1627 return m_Intrinsic<Intrinsic::canonicalize>(Op0);
1628}
1629
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001630template <typename Opnd0, typename Opnd1>
1631inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
1632 const Opnd1 &Op1) {
1633 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
1634}
1635
1636template <typename Opnd0, typename Opnd1>
1637inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
1638 const Opnd1 &Op1) {
1639 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
1640}
1641
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001642//===----------------------------------------------------------------------===//
1643// Matchers for two-operands operators with the operators in either order
1644//
1645
1646/// Matches a BinaryOperator with LHS and RHS in either order.
1647template <typename LHS, typename RHS>
1648inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
1649 return AnyBinaryOp_match<LHS, RHS, true>(L, R);
1650}
1651
1652/// Matches an ICmp with a predicate over LHS and RHS in either order.
1653/// Does not swap the predicate.
1654template <typename LHS, typename RHS>
1655inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
1656m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1657 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
1658 R);
1659}
1660
1661/// Matches a Add with LHS and RHS in either order.
1662template <typename LHS, typename RHS>
1663inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
1664 const RHS &R) {
1665 return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
1666}
1667
1668/// Matches a Mul with LHS and RHS in either order.
1669template <typename LHS, typename RHS>
1670inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
1671 const RHS &R) {
1672 return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
1673}
1674
1675/// Matches an And with LHS and RHS in either order.
1676template <typename LHS, typename RHS>
1677inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
1678 const RHS &R) {
1679 return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
1680}
1681
1682/// Matches an Or with LHS and RHS in either order.
1683template <typename LHS, typename RHS>
1684inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
1685 const RHS &R) {
1686 return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
1687}
1688
1689/// Matches an Xor with LHS and RHS in either order.
1690template <typename LHS, typename RHS>
1691inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
1692 const RHS &R) {
1693 return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
1694}
1695
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001696/// Matches a 'Neg' as 'sub 0, V'.
1697template <typename ValTy>
1698inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
1699m_Neg(const ValTy &V) {
1700 return m_Sub(m_ZeroInt(), V);
1701}
1702
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001703/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
1704template <typename ValTy>
1705inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
1706m_Not(const ValTy &V) {
1707 return m_c_Xor(V, m_AllOnes());
1708}
1709
1710/// Matches an SMin with LHS and RHS in either order.
1711template <typename LHS, typename RHS>
1712inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
1713m_c_SMin(const LHS &L, const RHS &R) {
1714 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
1715}
1716/// Matches an SMax with LHS and RHS in either order.
1717template <typename LHS, typename RHS>
1718inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
1719m_c_SMax(const LHS &L, const RHS &R) {
1720 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
1721}
1722/// Matches a UMin with LHS and RHS in either order.
1723template <typename LHS, typename RHS>
1724inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
1725m_c_UMin(const LHS &L, const RHS &R) {
1726 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
1727}
1728/// Matches a UMax with LHS and RHS in either order.
1729template <typename LHS, typename RHS>
1730inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
1731m_c_UMax(const LHS &L, const RHS &R) {
1732 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
1733}
1734
1735/// Matches FAdd with LHS and RHS in either order.
1736template <typename LHS, typename RHS>
1737inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
1738m_c_FAdd(const LHS &L, const RHS &R) {
1739 return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
1740}
1741
1742/// Matches FMul with LHS and RHS in either order.
1743template <typename LHS, typename RHS>
1744inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
1745m_c_FMul(const LHS &L, const RHS &R) {
1746 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
1747}
1748
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001749template <typename Opnd_t> struct Signum_match {
1750 Opnd_t Val;
1751 Signum_match(const Opnd_t &V) : Val(V) {}
1752
1753 template <typename OpTy> bool match(OpTy *V) {
1754 unsigned TypeSize = V->getType()->getScalarSizeInBits();
1755 if (TypeSize == 0)
1756 return false;
1757
1758 unsigned ShiftWidth = TypeSize - 1;
1759 Value *OpL = nullptr, *OpR = nullptr;
1760
1761 // This is the representation of signum we match:
1762 //
1763 // signum(x) == (x >> 63) | (-x >>u 63)
1764 //
1765 // An i1 value is its own signum, so it's correct to match
1766 //
1767 // signum(x) == (x >> 0) | (-x >>u 0)
1768 //
1769 // for i1 values.
1770
1771 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
1772 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
1773 auto Signum = m_Or(LHS, RHS);
1774
1775 return Signum.match(V) && OpL == OpR && Val.match(OpL);
1776 }
1777};
1778
1779/// Matches a signum pattern.
1780///
1781/// signum(x) =
1782/// x > 0 -> 1
1783/// x == 0 -> 0
1784/// x < 0 -> -1
1785template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
1786 return Signum_match<Val_t>(V);
1787}
1788
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001789} // end namespace PatternMatch
1790} // end namespace llvm
1791
1792#endif // LLVM_IR_PATTERNMATCH_H